Ari*_*ger 1 php assets routes laravel laravel-5
我正在开发一个函数来自动为每个视图获取资源(.css,.js).所以它可以说," http://mywebsite.com/displayitems ",/ home,/ about等.
但是自从我使用函数编写函数后$_SERVER['REQUEST_URI'],当我有一条路由时,我想出了一个问题,就像路由/displayitems/1中的"/ 1"一样.
那时候在Laravel 4.x我有一个很好的方法可以做到这一点,但遗憾的是它在Laravel 5.4中的工作方式不同.
我一直在网上搜索获得当前路线的好方法,但没有成功.问题是我必须忽略请求URL中的任何参数.
如果有人有线索,或者我做错了,并且有一种完全不同的,更好的方法吗?
PS我当前的功能:
public static function getAllRouteAssets() {
$route = $_SERVER['REQUEST_URI'];
if($route == "/") {
$tag = '<link href="' . asset("assets/css/pages/home.css") . '" rel="stylesheet" type="text/css"/>';
}
else {
// CSS
$tag = '<link href="' . asset("assets/css/pages" . $route . ".css") . '" rel="stylesheet" type="text/css"/>';
}
echo $tag;
//TODO: Check if file exists, homepage condition, js...
}
Run Code Online (Sandbox Code Playgroud)
你可以试试这个:
// Add the following (`use Illuminate\Http\Request`) statement at top your the class
public static function getAllRouteAssets(Request $request)
{
// Get the current route
$currentRoute = $request->route();
}
Run Code Online (Sandbox Code Playgroud)
更新(从IoC/Service容器获取请求实例并调用route()以获取当前路由):
app('request')->route(); // Current route has been retrieved
Run Code Online (Sandbox Code Playgroud)
如果要将当前路由作为参数传递给getAllRouteAssets方法,则必须更改typehint或传递Request并route从getAllRouteAssets方法中调用方法.