1. 自定义一个服务类

<?php
namespace App\Services;
class TestService
{
    public function testService()
    {
        return 'test service';
    }
}

2.在其它类或控制器中使用app()方法进行调用

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TestController extends Controller
{
    public function test()
    {
        // 也可以引入该类,此处改为 $service = app(TestService::class)
        $service = app('App\Services\Testservice');
        $result = $service->testService();
        dd($result);
    }
}