Laravel 5 how to get route action name?
In Laravel 5 you should be using Method or Constructor injection. This will do what you want:
<?php namespace App\Http\Controllers;
use Illuminate\Routing\Route;
class HomeController extends Controller
{
public function getIndex(Route $route)
{
echo 'getIndex';
echo $route->getActionName();
}
}
Laravel: How to Get Current Route Name? (v5 ... v7)
Try this
Route::getCurrentRoute()->getPath();
or
\Request::route()->getName()
from v5.1
use Illuminate\Support\Facades\Route;
$currentPath= Route::getFacadeRoot()->current()->uri();
Laravel v5.2
Route::currentRouteName(); //use Illuminate\Support\Facades\Route;
Or if you need the action name
Route::getCurrentRoute()->getActionName();
Laravel 5.2 route documentation
Retrieving The Request URI
The path method returns the request's URI. So, if the incoming request is targeted at http://example.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();
Laravel v5.3 ... v5.8
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
Laravel 5.3 route documentation
Laravel v6.x...7.x
$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();
** Current as of Nov 11th 2019 - version 6.5 **
Laravel 6.x route documentation
There is an option to use request to get route
$request->route()->getName();
How to get the action from a given laravel route name?
You could use the Route Facade.
\Route::getRoutes()->getByName('test')->getActionName()
// "App\Http\Controllers\TestController@index"
or the array with the whole action data:
\Route::getRoutes()->getByName('test')->getAction()
/*
[
"middleware" => "web",
"as" => "test",
"uses" => "App\Http\Controllers\TestController@index",
"controller" => "App\Http\Controllers\TestController@index",
"namespace" => "App\Http\Controllers",
"prefix" => null,
"where" => [],
]
*/
Get current route action name from middleware in laravel 5
You cannot get the route action name if the router has not yet been dispatched. The router class has not yet done its stuff - so you cannot do $router->request()
- it will just be null.
If it runs as routeMiddleware as $routeMiddleware
- then you can just do $router->request()
You can get the URI string in the middleware before the router has run - and do some logic there if you like: $request->segments()
. i.e. that way you can see if the URI segment matches a specific route and run some code.
Edit:
One way I can quickly think of is just wrap all your routes in a group like this:
$router->group(['middleware' => 'permissionsHandler'], function() use ($router) {
// Have every single route here
});
How to get name of requested controller and action in middleware Laravel
Laravel 5.6:
class_basename(Route::current()->controller);
Laravel 5.5 and lower:
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"
Laravel 5.x How to obtain the controller name using a route name?
As an example, this will give you information for the register
route:
Route::getRoutes()->getByName('register')->action;
This will give you an array of all the information you should need:
[
"middleware" => [
"web",
],
"uses" => "App\Http\Controllers\Auth\RegisterController@showRegistrationForm",
"controller" => "App\Http\Controllers\Auth\RegisterController@showRegistrationForm",
"namespace" => "App\Http\Controllers",
"prefix" => null,
"where" => [],
"as" => "register",
]
If you're doing this alot, you can add a macro in your RouteServiceProvider
:
public function register()
{
Route::macro('getByName', function($name) {
return $this->getRoutes()->getByName($name);
});
}
and now you can simply doRoute::getByName('register')
to get all the route information.
Laravel 5 - how to get action names (for getting urls in templates with route() function) from routes defined using Route::controllers()?
You can set the names for different controller actions when using Route::controller
:
Route::controller('auth', 'Auth\AuthController', [
'getLogin' => 'auth.login',
'getLogout' => 'auth.logout',
// and so on
]);
However you may also use the action()
helper instead of route()
or url()
. It let's you specify the controller and method you want to generate an URL for:
action('Auth\AuthController@getLogin')
Related Topics
Macd Function Returning Incorrect Values
Dompdf - Attach Created PDF to Email
Passing and Parse Paypal Ipn Custom Field
PHP - Find If an Array Contains an Element
Validate That Input String Does Not Exceed Word Limit
Codeigniter Command Line Error - PHP Fatal Error: Class 'Ci_Controller' Not Found
Hightest Value of an Associative Array
Php: How to Add Leading Zeros/Zero Padding to Float via Sprintf()
PHP $_Server['Php_Self'] to Include Query String
How to Decode a Base64 String (Gif) into Image in PHP/Html
Woocommerce: Display Also Product Variation Description on Cart Items
Can Mongodb and Its Drivers Preserve the Ordering of Document Elements
How to Avoid Using PHP Global Objects
Sort Array by Date in Descending Order by Date in PHP
How to Format a PHP Include() Absolute (Rather Than Relative) Path