1. Review the existing code, which is implemented based on the app function. The app function returns a service container instance. I cannot click getByName() to jump to the corresponding method. See Figure 1.

<?php
namespace App\Http\Middleware;
use App\Services\ThemeService;
use Carbon\Carbon;
use Closure;
use Illuminate\Support\Facades\Route;
class CurrentTheme
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$service = app(ThemeService::class);
// ...
$theme = $service->getByName($decrypt['name']);
// ...
return $next($request);
}
}2. Refer to the automatic injection of the service container. You can simply use “type-hinting” to inject dependencies that need to be resolved by the container in a class’s constructor, including controllers, event listeners, queue jobs, middleware, and so on. In fact, this is how most objects should be resolved by the container.
3. Reference URL: https://stackoverflow.com/questions/35439234/laravel-dependency-injection-in-middleware
4. Implement based on the constructor. I can now click getByName() to jump to the corresponding method. See Figure 2.

protected $themeService;
public function __construct(ThemeService $themeService)
{
$this->themeService = $themeService;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$theme = $this->themeService->getByName('vogue');
}
