简体   繁体   English

Laravel 8 如何给群命名

[英]Laravel 8 how to name a group

This question has been asked but it's outdated in most answers I saw.这个问题已经被问到,但在我看到的大多数答案中它已经过时了。

This is what I'm trying:这就是我正在尝试的:

Route::prefix('cart')->group(function() {
  Route::get('/mycart','App\Http\Controllers\frontend\CartController@mycart')->name('mycart');
  Route::get('/checkout','App\Http\Controllers\frontend\CartController@checkout')->name('checkout');
})->name('cart');

I'd like to name the group and then in my blade be able to do:我想命名该组,然后在我的刀片中能够做到:

@if (Route::currentRouteName() != 'individual_product')

The above gives me this error: Call to a member function name() on null上面给了我这个错误: Call to a member function name() on null

Any idea what i'm doing wrong?知道我做错了什么吗?

One can configure everything with arrays:可以使用 arrays 配置所有内容:

use Illuminate\Support\Facades\Route;

Route::group(['prefix' => 'cart', 'name' => 'cart.'], function() {
    Route::get('/index',    ['as' => 'index',    'uses' => 'CartController@index']);
    Route::get('/checkout', ['as' => 'checkout', 'uses' => 'CartController@checkout']);
});

And 'as' => 'cart.index' might still be more readable than a named group (it may depend).并且'as' => 'cart.index'可能仍然比 命名组更具可读性(这可能取决于)。


In Blade one can also useRoute :在 Blade 中也可以使用Route

@if (Route::has('cart')) @endif
@if (Route::has('cart.index')) @endif

Listing all defined routes is being supported:支持列出所有定义的路由:

php artisan route:list

You can use name prefixes like below:您可以使用如下name前缀:

Route::name('cart.')->prefix('cart')->group(function() {
  Route::get('/mycart','App\Http\Controllers\frontend\CartController@mycart')->name('mycart');
  Route::get('/checkout','App\Http\Controllers\frontend\CartController@checkout')->name('checkout');
});

for more: https://laravel.com/docs/8.x/routing#route-group-name-prefixes更多信息: https://laravel.com/docs/8.x/routing#route-group-name-prefixes

in blade you can check在刀片中你可以检查

@if(request()->routeIs('cart.*')) // or ->routeIs('cart.mycart')
@endif

https://laravel.com/docs/8.x/requests#retrieving-the-request-path https://laravel.com/docs/8.x/requests#retrieving-the-request-path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM