1、查看现有的代码,基于函数 app 实现。app 函数返回 服务容器 实例。无法通过点击 getByName() 跳转至对应的方法。如图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、参考服务容器的自动注入。可以简单地使用「类型提示」 的方式在类的构造函数中注入那些需要容器解析的依赖项,包括 控制器,事件监听器,队列任务,中间件,等 。实际上,这才是大多数对象应该被容器解析的方式。
3、参考网址:https://stackoverflow.com/questions/35439234/laravel-dependency-injection-in-middleware
4、基于构造函数实现。已可通过点击 getByName() 跳转至对应的方法。如图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');
}
PHP / Laravel / Yii2 老项目维护与长期技术支持
如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。
适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护
可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查
如需咨询,请联系我,并注明:PHP 维护咨询。
联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com
