get Route name in View
18,096
Solution 1
You may use this (to get current action, i.e. HomeController@index
)
Route::currentRouteAction();
This will return action
like HomeController@index
and you can use something like this in your view
<!-- echo the controller name as 'HomeController' -->
{{ dd(substr(Route::currentRouteAction(), 0, (strpos(Route::currentRouteAction(), '@') -1) )) }}
<!-- echo the method name as 'index' -->
{{ dd(substr(Route::currentRouteAction(), (strpos(Route::currentRouteAction(), '@') + 1) )) }}
The Route::currentRouteName()
method returns the name of your route that is used as 'as' => 'routename'
in your route declaration.
Solution 2
In Blade:
<p style="font-weight:{{ (Route::current()->getName() == 'admin.pages.index' && Request::segment(0) == 'add') ? 'bold' : 'normal' }};">Pages</p>
Solution 3
Request::segments()
will return an array with the current url for example:
yoursite/admin/users/create
will give:
array(2) {
[0] "admin"
[1] "users"
[2] "create"
}
Author by
MajAfy
Updated on July 27, 2022Comments
-
MajAfy almost 2 years
I trying to design navigation menu, I have 3 Items like this:
- Dashboard
- Pages
- List
- Add
- Articles
- List
- Add
Now I want to bold
Pages
when user is in this section,and if is in
Add
page I want bold bothPages
andAdd
my
routes.php
is :Route::group(array('prefix' => 'admin', 'before' => 'auth.admin'), function() { Route::any('/', 'App\Controllers\Admin\PagesController@index'); Route::resource('articles', 'App\Controllers\Admin\ArticlesController'); Route::resource('pages', 'App\Controllers\Admin\PagesController'); });
I found thid method :
$name = \Route::currentRouteName(); var_dump($name);
But this method return
string 'admin.pages.index' (length=17)
Should I use
splite
to get controller or Laravel have a method for this ? -
MajAfy about 10 yearsThanks, how can I use this in
view
? I mean is inBlade
templates -
diegofelix about 10 yearsThe Request is acessible in Views, but you can also pass it using the
with
method like: View::make('your-view')->with(array('activeController' => Request::segment(2))); or 'activeMenus' => Request::segments()