How to know the current route on Laravel 4?

Asked

Viewed 1,625 times

0

In Laravel 5, we can take the current route (I mean the class instance) using the class Illuminate\Http\Request, through the method route.

Thus:

 public function handle(Request $request)
 {
       $rota = $request->route();
 }

What about Laravel 4? How can I do this?

I need to know at a certain point what current route is being requested.

For example:

  App::before(function ($request)
  {
      $rota = /** Quero descobrir como pegar a rota atual aqui **/
  });

2 answers

2


Very good, Mr @Wallacemaxters! That’s very simple.

Just use the method Route::getCurrentRoute().

It is first important to remember that this will only work in filters and in the App::after. In the App::before will not work, because by logic, the route has not yet been processed, so that the instance is returned by Laravel.

Here comes an example:

 Route::filter('auth', function ()
 {
       dd(Route::getCurrentRoute()->getName());
 });

1

Route::getCurrentRoute()->getName()

The cool thing is that it also works in other versions (the ones bigger than the 4) of Laravel

  • But that was already answered, no?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.