欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

如何获得laravel路由行为的名字吗? 有大用

我想现在的路线行动,但我不知道如何去做。4我是用在laravelRoute::currentRouteAction()但现在有点不同

我想做的Route::getActionName()我的控制器,但它总是让我找不到方法。

<?php namespace App\Http\Controllers;

use Route;

class HomeController extends Controller
{
    public function getIndex()
    {
        echo 'getIndex';
        echo Route::getActionName();
    }
}
 
 

9个答案 正确答案

了投票十八投票表决认可的   

在laravel 5你应该使用的方法或构造函数注入。这将做你想做的事:

<?php namespace App\Http\Controllers;

use Illuminate\Routing\Route;

class HomeController extends Controller
{
    public function getIndex(Route $route)
    {
        echo 'getIndex';
        echo $route->getActionName();
    }
public function show($id,Request $request) {


     dd($request->route()->getActionName());

}
}
 
  
这部作品,伟大的,一个问题,如何在中间件做这个。比如我想自动设置“视图”在我的布局基于路线。我想在那里做同样的事情handle_ _构造的方法,但它不工作Unresolvable dependency resolving [Parameter #0 [ <required> $methods ]] in class Illuminate\Routing\Route抢劫11月11日14时54分
  
 
  
@ SzczepanHołyszewski法注射是一种标准的方式laravel,作品5。没有自动魔法它只是标准的依赖注入。劳伦斯4月22日21:01

把动作的名称,你需要使用:

echo Route::getCurrentRoute()->getActionName();

而不是

echo Route::getActionName();
分享提高这个答案
 
  
谢谢你,真的为我工作alsemany16年8月3日在10:38
  
我的结果是App\Http\Controllers\AdsController@create,所以它不仅动作名称。是否有可能得到它?格季米纳斯六月19 at 9:31

得到的路径动作名称中间件我做:

<?php
namespace App\Http\Middleware;

use Closure;
use Illuminate\Routing\Router;

class HasAccess {

    protected $router;

    public function __construct(User $user, Router $router)
    {
        $this->router = $router;
    }

    public function handle($request, Closure $next)
    {
        $action_name = $this->router->getRoutes()->match($request)->getActionName();
        //$action_name will have as value 'App\Http\Controllers\HomeController@showWelcome'
        //Now you can do what you want whit the action name 
        return $next($request);
    }
}

编辑:你会不会是这个中间件保护线路:(

分享提高这个答案
 

相反

use Illuminate\Routing\Route;

使用此

use Illuminate\Support\Facades\Route;

如果你想获得航线的别名,您可以使用:

Route::getCurrentRoute()->getName()
 
 

把动作名称只有(无控制器的名称):

list(, $action) = explode('@', Route::getCurrentRoute()->getActionName());
 

只有方法名称可以使用…

$request->route()->getActionMethod()

或一个门面…

Route::getActionMethod()
 

5.1使用用于laravel:

$route = new Illuminate\Routing\Route();
$route->getActionName(); // Returns App\Http\Controllers\MyController@myAction
$route->getAction(); // Array with full controller info

这类有用的方法很多。只是检查代码的更多细节。

 

在5.4只得到laravel动作名称

explode('@', Route::getCurrentRoute()->getActionName())[1]

找不到更好的办法,使用视图中的一行…

 

你可能用得到的控制器详细形式请求本身

$request->route()->getAction()
 

来自 https://stackoverflow.com/questions/26840278/laravel-5-how-to-get-route-action-name


I am new to Laravel, and i want to get name of requested controller and action in beforefilter middelware.

Thanks, DJ

 

1 Answer 正确答案

You can retrieve the current action name with Route::currentRouteAction(). Unfortunately this method will return a fully namespaced class name. So you will get something like:

App\Http\Controllers\FooBarController@method

Then just separate method name and controller name:

$currentAction = \Route::currentRouteAction();
list($controller, $method) = explode('@', $currentAction);
// $controller now is "App\Http\Controllers\FooBarController"

$controller = preg_replace('/.*\\\/', '', $controller);
// $controller now is "FooBarController"
 
   
Thanks, I was wondering if laravel has a inbuilt function for this, because it wil be an costly operation to do this for every request.... – Deejay May 25 '15 at 17:01
1 
There's no inbuilt functions for this as far as I know. – Limon Monte May 25 '15 at 17:03
1 
@limonte That is correct, there's no method implemented. In fact the runController method in Illuminate\Routing\Route uses explode in exactly the same way to determine the $class and $method that needs to be executed for the matched route. – Bogdan May 25 '15 at 17:14

来自 https://stackoverflow.com/questions/30442746/how-to-get-name-of-requested-controller-and-action-in-m...

I am trying to get current route name using Laravel 5.

In laravel 4 i was able to do it using Route::currentRouteName().

How can i do it in laravel 5?

Thanks

shareimprove this question
 
 

14 Answers 正确答案

 
up vote169down voteaccepted

try with this

Route::getCurrentRoute()->getPath();

or

\Request::route()->getName()

from v5.+

use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();

Laravel 5.3

Route::currentRouteName();

or if u need action name

Route::getCurrentRoute()->getActionName();

You can find everything about laravel Routes in Laravel API:http://laravel.com/api/5.0/Illuminate/Routing/Router.htmlhttp://laravel.com/api/5.0/Illuminate/Routing.html

Retrieving The Request URI

The path method returns the request's URI. So, if the incoming request is targeted at http://domain.com/foo/bar, the path method will return foo/bar:

$uri = $request->path();

The is method allows you to verify that the incoming request URI matches a given pattern. You may use the * character as a wildcard when utilizing this method:

if ($request->is('admin/*')) {
    //
}

To get the full URL, not just the path info, you may use the url method on the request instance:

$url = $request->url();
 
   
Do you have an idea how to filter this for instance if one only wants to print in the view api routes api/... – utdev Apr 21 at 15:34

Using Laravel 5.1, you can use

\Request::route()->getName()
shareimprove this answer
 
1 
this also works when you put it on the view as {{ route(\Request::route()->getName()) }} . Thanks so much! – bonbon.langes Feb 5 '16 at 12:57

Found a way to find the current route name works for laravel v5 , v5.1.28 and v5.2.10

Namespace

use Illuminate\Support\Facades\Route;

and

$currentPath= Route::getFacadeRoot()->current()->uri();

For Laravel laravel v5.3 you can just use:

use Illuminate\Support\Facades\Route;

Route::currentRouteName();
 
   
By the way, you don't need to import the entire namespace for a facade. use Route; will be enough. That's just one of the benefits of using facades in Laravel. – Jonathan Mar 23 at 11:00

If you need url, not route name, you do not need to use/require any other classes:

url()->current();
shareimprove this answer
 
   
This returns an error: "Call to a member function current() on a non-object". url() returns a string, not an object, so i dont think this could ever have worked. Perhaps you were thinking about some other method or object, instead of url()? – thelogix Apr 6 '16 at 21:24
1 
Nah, I use this on daily basis. Check official docs – Fusion Apr 8 '16 at 16:49
3 
I see. This only works in version 5.2 or greater. But its quite nice. – thelogix Apr 9 '16 at 10:12

If you want to selected menu on multiple routes you may do like this:

<li class="{{ (Request::is('products/*') || Request::is('products') || Request::is('product/*') ? 'active' : '') }}"><a href="{{url('products')}}"><i class="fa fa-code-fork"></i>&nbsp; Products</a></li>

Or if you want to selected just single menu you may simply do like this:

<li class="{{ (Request::is('/users') ? 'active' : '') }}"><a href="{{url('/')}}"><i class="fa fa-envelope"></i>&nbsp; Users</a></li>

Also tested in Laravel 5.2

Hope this help someone.

shareimprove this answer
 

You can use in template:

<?php $path = Route::getCurrentRoute()->getPath(); ?>
<?php if (starts_with($path, 'admin/')) echo "active"; ?>
shareimprove this answer
 

Laravel 5.2 You can use

$request->route()->getName()

It will give you current route name.

shareimprove this answer
 
6 
This is actually incorrect. the name() method will add or change the name, while the getName() method returns it. – Aron Rotteveel Apr 25 '16 at 9:01

Shortest way is Route facade \Route::current()->getName()

shareimprove this answer
 

In a controller action, you could just do:

public function someAction(Request $request)
{
    $routeName = $request->route()->getName();
}

$request here is resolved by Laravel's service container.

getName() returns the route name for named routes only, null otherwise (but you could still explore the \Illuminate\Routing\Route object for something else of interest).

In other words, you should have your route defined like this to have "nameOfMyRoute" returned:

Route::get('my/some-action', [
    'as' => 'nameOfMyRoute',
    'uses' => 'MyController@someAction'
]);
shareimprove this answer
 
   
Not working in Laravel 5.0 – happy_marmoset Jan 19 '16 at 7:50

Request::path(); is better, and remember to Use Request;

shareimprove this answer
 

In 5.2, you can use the request directly with:

$request->route()->getName();

or via the helper method:

request()->route()->getName();

Output example:

"home.index"
shareimprove this answer
 

Now in Laravel 5.3 I am seeing that can be made similarly you tried:

$route = Route::current();

$name = Route::currentRouteName();

$action = Route::currentRouteAction();

https://laravel.com/docs/5.3/routing#accessing-the-current-route

shareimprove this answer
 

In a Helper file,

Your can use Route::current()->uri() to get current URL.

Hence, If you compare your route name to set active class on menu then it would be good if you use

Route::currentRouteName() to get the name of route and compare

shareimprove this answer
 

I have used for getting route name in larvel 5.3

Request::path()

 

来自 https://stackoverflow.com/questions/30046691/how-to-get-current-route-name-in-laravel-5


普通分类: