Laravel 5.1 - 获取当前路线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31500988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1 - get current route
提问by Ariel Weinberger
I'm working on a function to get assets (.css, .js) automatically for each view. So it works fine for let's say, "http://mywebsite.com/displayitems", /home, /about etc.
我正在开发一个函数来为每个视图自动获取资产(.css、.js)。所以它适用于“ http://mywebsite.com/displayitems”、/home、/about等。
But since I wrote the function using $_SERVER['REQUEST_URI']
, I came up with an issue when I had a route like /displayitems/1
because of the "/1" in the route.
但是由于我使用 编写了该函数$_SERVER['REQUEST_URI']
,所以当我有一个路由时,我遇到了一个问题,比如路由/displayitems/1
中的“/1”。
Back then in Laravel 4.x I had a great way to do it but sadly it doesn't work the same way in Laravel 5.4.
当时在 Laravel 4.x 中我有一个很好的方法来做到这一点,但遗憾的是它在 Laravel 5.4 中的工作方式不同。
I've been searching through the internet for a good method to get the current route but no success. The thing is that I have to ignore any parameters in the request URL.
我一直在通过互联网搜索获取当前路线的好方法,但没有成功。问题是我必须忽略请求 URL 中的任何参数。
If anyone has a clue, or maybe am I doing it wrong and there's a completely different, better way to do it?
如果有人有线索,或者我做错了,有一种完全不同的更好的方法来做到这一点?
P.S My current function:
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...
}
回答by The Alpha
You may try this:
你可以试试这个:
// 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();
}
Update (Get the Request instance from IoC/Service container and call route()
to get current route):
更新(从 IoC/Service 容器获取 Request 实例并调用route()
以获取当前路由):
app('request')->route(); // Current route has been retrieved
If you want to pass the current route as a parameter to your getAllRouteAssets
method then you have to change the typehint
or pass the Request
and call the route
method from within the getAllRouteAssets
method.
如果要将当前路由作为参数传递给getAllRouteAssets
方法,则必须更改typehint
或 传递Request
并从route
方法内调用该getAllRouteAssets
方法。
回答by Sterex
I know this is a bit old, but there is a method that gives you the entire query path:
我知道这有点旧,但是有一种方法可以为您提供整个查询路径:
$request->getPathInfo();
$request->getPathInfo();
However, note that this will not work if you are looking to fetch the query string as well. (FYI, Laravel 5 does not support query strings by default)
但是,请注意,如果您还想获取查询字符串,这将不起作用。(仅供参考,Laravel 5 默认不支持查询字符串)
You can individually fetch the GET variables from the query strings by:
您可以通过以下方式从查询字符串中单独获取 GET 变量:
$request->input('id');
$request->input('id');
Example:
例子:
http://laravel.com/api/users/?id=123
would return /api/users
using getPathInfo()
and 123
using $request->input('id');
http://laravel.com/api/users/?id=123
会返回/api/users
使用getPathInfo()
和123
使用$request->input('id');
回答by Igor Zec
I am using Laravel 5.5.20. I had also need to get part of route without get parameters. Routes are defined without question mark(?) in web.php, as for example:
我正在使用 Laravel 5.5.20。我还需要在没有获取参数的情况下获取部分路线。路由在 web.php 中定义时不带问号(?),例如:
Route::get('board/{param_1}/{param_2}', 'BoardController@index');
In that case I did not see direct method in Route class to get part without url parameters. Here is how I got static part (/board):
在那种情况下,我没有在 Route 类中看到直接方法来获取没有 url 参数的部分。这是我获得静态部分(/board)的方法:
...
use Illuminate\Support\Facades\Route;
..
$staticPrefix = Route::getCurrentRequest()->route()->getCompiled()->getStaticPrefix();
...
...
use Illuminate\Support\Facades\Route;
..
$staticPrefix = Route::getCurrentRequest()->route()->getCompiled()->getStaticPrefix();
...