首页 文章

如何从laravel 5.3中的url获取路由前缀名称

提问于
浏览
-1

如何在Laravel 5.3中动态获取路由前缀名称?

例如,这是URL:

http:// localhost / lara / public / admin / login

这是路径文件 . 路由组前缀为 admin

Route::group(['prefix' => 'admin'], function () {
    Route::get('users', function () {
        // Matches "/admin/users"
    });
});

1 回答

  • 0

    你要问的是什么并不是很清楚,但我想你希望能够动态配置你的路由组前缀 . 例如,能够动态更改管理区域的URI前缀 .

    让我们为 config/app.php 文件添加一个相应的配置指令:

    return [
        // ...
    
        'admin_url' => 'backoffice',
    ];
    

    然后在你的路线文件中:

    // Fetch the prefix from the config file, fallback to "admin" if not available
    Route::group(['prefix' => config('app.admin_url', 'admin')], function () {
        Route::get('users', function () {
            // ...
        });
    });
    

    然后在控制器中,您可以使用对 config() helper或 $request->route()->getPrefix() 的相同调用来访问定义的路由前缀 .

    如果上下文中没有 $request 实例,则可以在任何控制器中使用 $this->getRouter()->getCurrentRoute()->getPrefix() 获取前缀 .

相关问题