0

I need to get the action name in laravel:

mysite.com/manager/user-list

So in the above example I want 'user-list'

I know you can get the current route name by:

Route::currentRouteName();

But this is no good to me, I need this to stay as 'manager' for other reasons.

3
  • It may be worth explaining why you need this - you might get a better answer than simply a direct answer to your question. Commented Feb 25, 2014 at 15:01
  • I need it so I can name my pages with the current action name, so I can then load in the relevant CSS. I can't use the current route name or rename each individual route as this is used to load in conditional JS. Commented Feb 25, 2014 at 15:23
  • I suppose it relies on a few more details. Are your pages not models but instead 'static' view files? If they are models, then it's best to rely on something in there (id, slug) than the request segments. If they are static views, you could maybe use @section() tags in your blade views to add your scripts. What does your routes.php file look like (and the controller code that creates the views for these pages)? Commented Feb 25, 2014 at 15:38

3 Answers 3

2

If you want to get URL segment (not controller action), you can call segment method on Request Facade.

Request::segment(2);
Sign up to request clarification or add additional context in comments.

2 Comments

This does not get the action name, just the url.
@panthro: Your example is about URLs and not actions. They often relate, but by supplying the URL instead of the routing rules, you're not asking for help getting the action.
1

i don't know what you are trying to do but this is one of the methods to get the action.

print_r(explode('@',Route::currentRouteAction()));

Output

Array ( [0] => HomeController [1] => index )

so 1st element will hold the controller (provided not namespaced, otherwise you will have to remove the namespace part manuelly) and 2nd element will hold the action.

your question and the explanation doesn't match.

if you want the action, this is one of the process.

but i think, Andreyco's answer gives you what you need. i just put it for future reference if someone finds it helpful.

Comments

1

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

The link above shows you how to solve your problem:

$action = Route::currentRouteAction();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.