Laravel 路由命名错误:未按预期获取资源路由命名

问题描述 投票:0回答:1

我面临 Laravel / React.js 应用程序的问题

我有这个 web.route 配置:

Route::middleware(['auth', 'verified'])->group(function () {
    Route::redirect('/admin', '/admin/dashboard');
    Route::get('/admin/dashboard', [DashboardController::class, 'index'])->name('adminDashboard');
    Route::resource('/admin/properties', PropertyController::class);
    Route::resource('/admin/property-types/', PropertyTypeController::class);
    Route::resource('/admin/property-meta', PropertyMetaController::class);
    Route::resource('/admin/transaction-types', TransactionTypeController::class);
    Route::resource('/admin/amenities', AmenityController::class);
    Route::resource('/admin/posts', PostController::class);
    Route::resource('/admin/post-categories', PostCategoryController::class);
    Route::resource('/admin/bookings', BookingController::class);
    Route::resource('/admin/users', UserController::class);
    Route::resource('/admin/roles', RoleController::class);
    Route::resource('/admin/permissions', PermissionController::class);
});

路线:列表输出是:

PUT|PATCH       admin/property-meta/{property_metum} ......................... property-meta.update › Admin\PropertyMetaController@update  
  DELETE          admin/property-meta/{property_metum} ....................... property-meta.destroy › Admin\PropertyMetaController@destroy  
  GET|HEAD        admin/property-meta/{property_metum}/edit ........................ property-meta.edit › Admin\PropertyMetaController@edit  
  GET|HEAD        admin/property-types ......................................................... index › Admin\PropertyTypeController@index  
  POST            admin/property-types ......................................................... store › Admin\PropertyTypeController@store  
  GET|HEAD        admin/property-types/create ................................................ create › Admin\PropertyTypeController@create  
  GET|HEAD        admin/property-types/{} ........................................................ show › Admin\PropertyTypeController@show  
  PUT|PATCH       admin/property-types/{} .................................................... update › Admin\PropertyTypeController@update  
  DELETE          admin/property-types/{} .................................................. destroy › Admin\PropertyTypeController@destroy  
  GET|HEAD        admin/property-types/{}/edit ................................................... edit › Admin\PropertyTypeController@edit  
  GET|HEAD        admin/roles .................................................................... roles.index › Admin\RoleController@index  
  POST            admin/roles .................................................................... roles.store › Admin\RoleController@store  
  GET|HEAD        admin/roles/create ........................................................... roles.create › Admin\RoleController@create 

在输出中,您可以看到 property-types/create 路由名称是“create”,而第一个名称是“property-types.create”

我尝试通过将路线重新定义为来修复它:

Route::resource('/admin/property-types/', PropertyTypeController::class)->names('property-types');

它有效,但是我为编辑路线生成了奇怪的 url。它给出了这个网址:

route('property-types.edit', propertyType.id)

http://realestate-laravel/admin/property-types/%7B%7D/edit?6=

我尝试添加带有名称索引的假路由以强制 propertyTypes 不使用此路由,但我无法编译路由缓存。

Route::middleware(['auth', 'verified'])->group(function () {
    Route::redirect('/admin', '/admin/dashboard');
    Route::get('/admin/dashboard', [DashboardController::class, 'index'])->name('adminDashboard');
    Route::get('/admin/x', [DashboardController::class, 'index'])->name('index'); //here! forcing this route to take the 'index' route
    Route::resource('/admin/properties', PropertyController::class);
    Route::resource('/admin/property-types/', PropertyTypeController::class); //this rule has no names defined. should take property-types.index .create .store ...
    Route::resource('/admin/property-meta', PropertyMetaController::class);
    Route::resource('/admin/transaction-types', TransactionTypeController::class);
    Route::resource('/admin/amenities', AmenityController::class);
    Route::resource('/admin/posts', PostController::class);
    Route::resource('/admin/post-categories', PostCategoryController::class);
    Route::resource('/admin/bookings', BookingController::class);
    Route::resource('/admin/users', UserController::class);
    Route::resource('/admin/roles', RoleController::class);
    Route::resource('/admin/permissions', PermissionController::class);
});

运行路线时:缓存我得到这个:

  Unable to prepare route [admin/property-types] for serialization. Another route has already been assigned name [index].

  at vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php:247
    243▕             $route->name($this->generateRouteName());
    244▕
    245▕             $this->add($route);
    246▕         } elseif (! is_null($symfonyRoutes->get($name))) {
  ➜ 247▕             throw new LogicException("Unable to prepare route [{$route->uri}] for serialization. Another route has already been assigned name [{$name}].");
    248▕         }
    249▕
    250▕         $symfonyRoutes->add($route->getName(), $route->toSymfonyRoute());
    251▕

  1   vendor\laravel\framework\src\Illuminate\Routing\AbstractRouteCollection.php:208
      Illuminate\Routing\AbstractRouteCollection::addToSymfonyRoutesCollection(Object(Symfony\Component\Routing\RouteCollection), Object(Illuminate\Routing\Route))

  2   vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php:247
      Illuminate\Routing\AbstractRouteCollection::toSymfonyRouteCollection()
routes laravel-8 ziggy
1个回答
0
投票

现已修复!

问题是路线名称末尾有一个反斜杠。

Route::resource('/admin/property-types', PropertyTypeController::class);

修复

Route::resource('/admin/property-types/', PropertyTypeController::class);
© www.soinside.com 2019 - 2024. All rights reserved.