PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 26, 2022

[FIXED] How to define route group name in laravel

 February 26, 2022     laravel, php, routes     No comments   

Issue

Is there any way to define the name of route group in laravel?

What I'm trying to accomplish by this is to know that the current request belongs to which group so I can make active the main menu and sub menu by the current route action:

Code:

Route::group(['prefix'=>'accounts','as'=>'account.'], function(){
    Route::get('/', 'AccountController@index')->name('index');
    Route::get('connect', 'AccountController@connect')->name('connect');
});

Route::group(['prefix'=>'quotes','as'=>'quote.'], function(){
    Route::get('/', 'QuoteController@index')->name('index');
    Route::get('connect', 'QuoteController@create')->name('create');
});

Navigation HTML Code

<ul>
    <li> // Add class 'active' when any route is open from account route group
        <a href="{{route('account.index')}}">Accounts</a>
        <ul>
            <li> // Add class 'active' when connect sub menu is clicked
                <a href="{{route('account.connect')}}">Connect Account</a>
            </li>
        </ul>
    </li>
    <li> // Add class 'active' when any route is open from quote route group
        <a href="{{route('quote.index')}}">Quotes</a>
        <ul>
            <li> // Add class 'active' when create sub menu is clicked
                <a href="{{route('quote.create')}}">Create Quote</a>
            </li>
        </ul>
    </li>
</ul>

Now what I want is to call a function or something which will give me the current route's group name.

Examples:

  1. If I'm on index or create page of quotes getCurrentRouteGroup() should return quote
  2. If I'm on index or connect page of accounts getCurrentRouteGroup() should return account

Solution

This should work:

Route::group(['prefix'=>'accounts','as'=>'account.'], function(){
    Route::get('/', ['as' => 'index', 'uses' => 'AccountController@index']);
    Route::get('connect', ['as' => 'connect', 'uses' = > 'AccountController@connect']);
});

Look here for an explanation and in the official documentation (under Route Groups & Named Routes).

Update

{{ $routeName = \Request::route()->getName() }}

@if(strpos($routeName, 'account.') === 0)
    // do something
@endif

Alternative from Rohit Khatri

function getCurrentRouteGroup() {
    $routeName = Illuminate\Support\Facades\Route::current()->getName();
    return explode('.',$routeName)[0];
}


Answered By - Alexey Mezenin
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing