小编就为大家分享一篇laravel 获取当前url的别名方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

Route::get('/xiongtest', [
  'as' => 'xiong.test',
  'uses' => [email protected]'
]);

以上路由为例

在模版中可以使用route(‘xiong.test’)来获取该路由的真实地址。

[email protected] ,可以使用以下方法获取路由别名

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\[email protected]"
 "controller" => "App\Http\Controllers\Home\Main\[email protected]"
 "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();
}