0

I want to resolve the controller name and action which are configured for a route

I have a route:

Route::get('/somePage','SomeController@someAction'); 

Can I get the controller name and action using something like:

resolve('/somepage'); 

to receive the same result which I can get from Route::current()->getActionName()

App\Http\Controllers\SomeController@someAction 
6
  • Do you have any issues with Route::current()->getActionName()? Commented Aug 30, 2019 at 12:43
  • There is route() helper by the way Commented Aug 30, 2019 at 12:44
  • 1
    This looks like an XY problem. Why do you need to do this? Are you sure there's no better solution to your original problem? Commented Aug 30, 2019 at 12:58
  • I'm building authorization system and I want to check if the user has permission for specific URL before the page is resolved. that is why I want to resolve the name of the controller and the action to see if the user has permissions for the page Commented Aug 30, 2019 at 13:01
  • IMHO request()->route()->getAction() is enough for nearly everybody, you can call it on the controller constructor or in a middleware. There is a reason if a function like you asked does not exist yet in the laravel codebase: nobody needs it. And you did not explained why do you need it. So until explained it is an XY problem for me also. Commented Aug 30, 2019 at 13:14

3 Answers 3

5

This should work:

function getAction($uri, $method) { $route = collect(Route::getRoutes()) ->filter(function($route) use($uri, $method){ return $route->getUri() === $uri && in_array($method, $route->getMethods()); })->first(); return $route ? $route->getAction() : null; } 

Usage:

$action = getAction('posts', 'GET'); 

Alternatively:

$request = \Illuminate\Http\Request::create('posts', 'GET'); $action = Route::getRoutes()->match($request)->getAction(); 
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, this solved my problem. The only problem was that $route->getUri() doesn't exist and $route->url have to be used. The alternative solution doesn't work for me.
@PlamenVasilev What version of Laravel are you running?
You can check what API's are supported - laravel.com/api/5.1/Illuminate/Routing/Route.html
0

I usually use route('somePageRoute') method but first name the route

Route::get('/somePage','SomeController@someAction')->name('somePageRoute'); 

1 Comment

route() function returns a url like http://domain/someAction I want the controller name and the action name, not the URL
-1

Simply you can get controller name and action by this

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

1 Comment

I want it for specific route, not for the current one!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.