Laravel如何获取当前URL的别名?
- 内容介绍
- 文章标签
- 相关推荐
本文共计320个文字,预计阅读时间需要2分钟。
如所示:`Route::get('/xiongtest', ['as'=> 'xiong.test', 'uses'=> 'XiongTestController@index']);` 以上路由示例中,在路由配置中使用了 `as` 和 `uses` 键值对来定义路由别名和控制器方法。在路由中,可以使用 `route('xiong.test')` 来获取该路由的真实地址。在 `XiongTestController@index` 中,可以访问相关逻辑。
如下所示:
Route::get('/xiongtest', [ 'as' => 'xiong.test', 'uses' => 'XiongTestController@index' ]);
以上路由为例
在模版中可以使用route('xiong.test')来获取该路由的真实地址。
在XiongTestController@index中,可以使用以下方法获取路由别名
public function index(Request $request) $routeAction = $request->route()->getAction(); print_r($routeAction); }
输出结果为:
array:8 [▼ "domain" => "www.laravelylw.com" "middleware" => array:5 [▶] "as" => "xiong.test" "uses" => "App\Http\Controllers\Home\Main\XiongTestController@index" "controller" => "App\Http\Controllers\Home\Main\XiongTestController@index" "namespace" => "App\Http\Controllers\Home\Main" "prefix" => null "where" => [] ]
或者使用getName()方法直接获取别名
$request->route()->getName()
或者用
use Illuminate\Routing\Route; public function index(Request $request,Route $route) { echo $route->getName(); }
以上这篇laravel 获取当前url的别名方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计320个文字,预计阅读时间需要2分钟。
如所示:`Route::get('/xiongtest', ['as'=> 'xiong.test', 'uses'=> 'XiongTestController@index']);` 以上路由示例中,在路由配置中使用了 `as` 和 `uses` 键值对来定义路由别名和控制器方法。在路由中,可以使用 `route('xiong.test')` 来获取该路由的真实地址。在 `XiongTestController@index` 中,可以访问相关逻辑。
如下所示:
Route::get('/xiongtest', [ 'as' => 'xiong.test', 'uses' => 'XiongTestController@index' ]);
以上路由为例
在模版中可以使用route('xiong.test')来获取该路由的真实地址。
在XiongTestController@index中,可以使用以下方法获取路由别名
public function index(Request $request) $routeAction = $request->route()->getAction(); print_r($routeAction); }
输出结果为:
array:8 [▼ "domain" => "www.laravelylw.com" "middleware" => array:5 [▶] "as" => "xiong.test" "uses" => "App\Http\Controllers\Home\Main\XiongTestController@index" "controller" => "App\Http\Controllers\Home\Main\XiongTestController@index" "namespace" => "App\Http\Controllers\Home\Main" "prefix" => null "where" => [] ]
或者使用getName()方法直接获取别名
$request->route()->getName()
或者用
use Illuminate\Routing\Route; public function index(Request $request,Route $route) { echo $route->getName(); }
以上这篇laravel 获取当前url的别名方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

