【问题标题】:How to define route group name in laravel如何在 laravel 中定义路由组名称
【发布时间】:2016-08-18 16:51:51
【问题描述】:

有什么方法可以在 laravel 中定义路由组的名称吗?

我试图通过这个来完成的是知道当前请求属于哪个组,这样我就可以通过当前路由操作激活主菜单和子菜单:

代码:

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');
});

导航 HTML 代码

<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>

现在我想要的是调用一个函数或其他东西,它会给我当前路由的组​​名。

例子:

  1. 如果我在索引或创建引用页面getCurrentRouteGroup() 应该返回quote
  2. 如果我在索引或帐户连接页面上getCurrentRouteGroup() 应该返回account

【问题讨论】:

    标签: php laravel routes


    【解决方案1】:

    这应该可行:

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

    Look here 用于解释并在 official documentation(在 Route Groups & Named Routes 下)。

    更新

    {{ $routeName = \Request::route()->getName() }}
    
    @if(strpos($routeName, 'account.') === 0)
        // do something
    @endif
    

    Rohit Khatri 的替代品

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

    【讨论】:

    • 我想在视图上获取当前路由组名。
    • 使用相同的{{ route('account.index') }}
    • 它给了我路由 url,我不想要 url。我想调用一个函数getCurrentRouteGroup(),它会给我路由组的名称。就像如果我在帐户的连接或索引页面上,那么getCurrentRouteGroup() 将返回account
    • 你没有在你原来的问题中问过这个问题,但是请看我回答的更新部分。
    • 感谢您的回答。稍微修正一下 - 最好使用@php $routeName = \Request::route()-&gt;getName() @endphp,因为{{}} 会输出变量。
    【解决方案2】:

    试试这个

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

    【讨论】:

    • 我的工作很完美,但我想知道请求属于哪个组。
    【解决方案3】:
    // both the format of defining the prefix are working,tested on laravel 5.6
    
    Route::group(['prefix'=>'accounts','as'=>'account.'], function() {
        Route::get('/', 'SomeController@index')->name('test');
        Route::get('/new', function(){
                return redirect()->route('account.test');
        });
    });
    
    Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
        Route::get('/', [
            'as' => 'custom',
            'uses' => 'SomeController@index'
        ]);  
    
        Route::get('/custom', function(){
            return route('admin.custom');
        });
    }); 
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    【解决方案4】:

    它应该可以工作-

    刀片内部-

    {{ $yourRouteName = \Request::route()->getName() }}
    
    // Find the first occurrence of account in URL-
    
    @if(strpos($routeName, 'account.') === 0)
      console the message or your code
    @endif
    

    【讨论】:

      【解决方案5】:

      您可以使用Route::name()-&gt;group(...) 为一组路由的所有名称添加前缀

      Route::name('foo.')->prefix('xyz')->group(function() {
      
          Route::get('path', 'SomeController@method')->name('bar');
      
      });
      

      这里 route('foo.bar') 解析为 url /xyz/path

      查看相关Laravel Docs

      不要忘记在前缀名称中添加点:-)

      【讨论】:

      • 我认为这是最好的答案,因为代码干净简洁。
      【解决方案6】:

      在 Laravel 9 中,您现在可以这样做:

      Route::controller(AccountController::class)->group(function () {

      Route::get('/', 'index')->name('index');
      Route::get('/connect', 'connect')->name('connect');
      

      });

      【讨论】:

      • 这与旧版本提供的相同。请分享一些新的解决方案。
      猜你喜欢
      • 2022-11-26
      • 2018-03-21
      • 2018-03-19
      • 2015-02-03
      • 2018-11-09
      • 1970-01-01
      • 2019-12-12
      • 2020-03-20
      • 2017-04-01
      相关资源
      最近更新 更多