访问 app/providers 中的路由前缀

accessing route prefix in app/providers

我有一个多模板网站,我在我的视图文件夹中为不同的模板创建了多个文件夹...但我有 1 个管理模板,我想从该文件夹加载所有管理部分

在我的路线中,我有类似

的东西
Route::group(['namespace' => 'Cp', 'prefix' => 'cp'  , 'middleware'=>['admin' ,'auth'] ], function()
{

    Route::get('/' ,  'IndexController@index' )->name('index_cp');
});

app/providers/ViewServiceProvider.php 中,我根据数据库存储的值动态更改模板文件夹,但我试图获取当前路由的前缀,因此如果它 cp 它忽略模板文件夹并从 cp文件夹

    public function registerViewFinder()
    {
        $request = app(\Illuminate\Http\Request::class);
        dump($request->route()->getPrefix());
        if($request->route()->getPrefix() != 'cp')
        {
         // read from db &  set the template folder 
        }

    }

我收到这个错误

Call to a member function getPrefix() on null

基本上$request->route()returns无

我认为您可以在请求对象上使用 is() 方法,而不是采用 getPrefix 方法,

其中 is 方法执行以下操作

The is method allows you to verify that the incoming request URI matches a given pattern. You may use the * character as a wildcard when utilizing this method:

if (request()->is('cp/*')) {
   // do the template switching.
}

doc link