php - 使用jetstream导航菜单找不到Laravel 8路线
问题描述
对 laravel 8 来说相当新,对 laravel 7 有一些经验。我正在尝试将一些额外的页面添加到默认的仪表板导航菜单中。但是,在添加代码之后,正如我所期望的那样,我收到了这个错误:
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [accounts.index] not defined. (View: /home/some/path/resources/views/navigation-menu.blade.php)
所以这就是我在代码方面所做的事情:在 web.php 我有以下路线:
Route::middleware(['auth:sanctum', 'verified'])
->get('/accounts', [AccountController::class, 'index'])
->name('accounts');
我有一个控制器 /app/Http/Controllers/AccountController.php 如下:
<?php
namespace App\Http\Controllers;
use App\Models\Account;
use Illuminate\Support\Facades\View;
class AccountController extends Controller
{
//
public function index() {
$accounts = Account::all();
return View::make('pages.accounts.index')->with('accounts', $accounts);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
return View::make('pages.accounts.create');
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
我有一个模型/app/Models/Account.php:
<?php
namespace App\Models;
use Eloquent;
class Account extends Eloquent
{
}
我也有此页面的刀片模板,为了简洁起见,我不会列出这些模板,因为我认为这与问题无关,此时如果我导航到 {url}/accounts,帐户索引页面将按预期显示。
但是现在我想将此功能移动到 jetstream 仪表板中,这样我就可以减少一些开发时间并将其主题化,类似于默认的 laravel 概念。
我需要的第一件事是在仪表板旁边添加一个新的导航项,所以我通过复制它用于仪表板的内容来修改默认的导航菜单.blade.php 文件 (/resources/views/navigation-menu.blade.php) 和更新:
...
<!-- Navigation Links -->
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<x-jet-nav-link href="{{ route('dashboard') }}" :active="request()->routeIs('dashboard')">
{{ __('Dashboard') }}
</x-jet-nav-link>
</div>
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<x-jet-nav-link href="{{ route('accounts.index') }}" :active="request()->routeIs('accounts.index')">
{{ __('Accounts') }}
</x-jet-nav-link>
</div>
</div>
...
正是在此时重新加载页面时引发错误。有谁知道这是什么原因造成的?我最初尝试不使用 .index 因为这是默认值,对吗?
我也尝试过这样做((https://eheidi.dev/blog/creating-a-multi-user-to-do-application-with-laravel-jetstream-2p1k)),但我得到了当我在编辑navigation-manu.blade.php 文件后重新加载我的页面时出现同样的错误,所以我很茫然。我正在 ubuntu 20.04 上开发这个
谢谢
克雷格
*** 编辑 *** 我已经更新了我的路线,以便更好地使用前进的资源。
Route::middleware(['auth:sanctum', 'verified'])
->resource('/accounts', [AccountController::class, 'index'])
->name('accounts.index');
在没有修改navigation-menu.blade.php 的情况下对此进行了测试,并且所有仍然有效,添加了修改和相同的错误。
*** 编辑 2 *** 我认为将其缩小到导航菜单文件和网络路由文件中应该包含的内容。我根据 Chadrack 下面的回答的第 2 点进一步修改了我的 web.php 代码:
Route::middleware(['auth:sanctum', 'verified'])
->resource('/accounts', AccountController::class)
->only( ['index', 'create', 'store', 'update'])
->name('index', 'accounts');
我添加到 navigation-menu.blade.php 的片段是:
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<x-jet-nav-link href="{{ route('accounts') }}" :active="request()->routeIs('accounts')">
{{ __('Accounts') }}
</x-jet-nav-link>
</div>
这仍然会引发错误,如果我将 .index 添加到路由函数( route('accounts.index') )我仍然会收到错误(Route [accounts.index] 未定义或 Route [accounts] 未定义
*** 编辑 3 *** 我重复了我尝试过但失败的初始教程(https://eheidi.dev/blog/creating-a-multi-user-to-do-application-with-laravel-jetstream- 2p1k)但与我的问题一样,在我将 x-jet nav-link 部分添加到刀片模板并添加路线之后,当我重新加载时,我得到相同的错误(使用新定义的路线)路线 [dashboard-todo]没有定义的。因此,如果两个选项的问题相同,那么我必须在这里遗漏一些东西吗?正如之前所指出的,我已经尝试过每种路线类型的组合。它使用 url [url]/accounts 工作,直到我将链接添加到导航栏中。当没有对 navigation-menu.blade.php 进行任何更改时,仪表板确实可以工作
解决方案
您需要使用它的名称来调用您的路线。
在您的定义中accounts
,您在调用名为的路由时使用名称定义路由accounts.index
替换你的路线
Route::middleware(['auth:sanctum', 'verified'])
->get('/accounts', [AccountController::class, 'index'])
->name('accounts');
经过
Route::middleware(['auth:sanctum', 'verified'])
->get('/accounts', [AccountController::class, 'index'])
->name('accounts.index');
// note this
推荐阅读
- python - Python 中的 CRC-8 用于一个字节 (crcmod)
- python - 活动覆盖重定向 tkinter 窗口的任务栏图标
- php - 子查询为此查询返回多行
- python - 如何在不删除重复项的情况下进行排序?
- codenameone - 如何让 Codenameone android App 可靠地连接到 REST API?
- javascript - 将元素放置在与数组中的元素相同的索引中
- python - 如何在模型中引发 HTTP 400?
- python - Selenium WebDriver:我想覆盖字段中的值,而不是使用 python 使用 sendKeys 附加到它
- java - 防止内存映射文件变得太大
- mysql - DELETE 的目标表 Result 不可更新。如何解决?