遇到了"make:controller controller_name --resource"命令運行時的路由未定義錯誤
P粉983021177
P粉983021177 2024-04-02 19:47:45
0
1
405

web.php

#
<?php

use Illuminate\Support\Facades\Route;


use App\Http\Controllers\HomeController;
use App\Http\Controllers\PostsController;
use App\Http\Controllers\AboutController;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\CategoryController;
use App\Http\Controllers\TagController;
use App\Http\Controllers\AdminControllers\DashboardController;
use App\Http\Controllers\AdminControllers\AdminPostsController;
use App\Http\Controllers\AdminControllers\AdminCategoriesController;
use App\Http\Controllers\AdminControllers\TinyMCEController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


Route::get('/', [HomeController::class,'index'])->name("home");

Route::get('/posts/{post:slug}',[PostsController::class,'show'])->name("posts.show");
Route::post('/posts/{post:slug}',[PostsController::class,'addComment'])->name("posts.add_comment");

Route::get('/contact', [ContactController::class , 'create'])->name("contact.create");
Route::post('/contact', [ContactController::class , 'store'])->name("contact.store");

Route::get('/about', AboutController::class)->name("about");

Route::get('/categories/{category:slug}',[CategoryController::class,'show'])->name("categories.show");
Route::get('/categories',[CategoryController::class,'index'])->name("categories.index");
///  /tags/{tag:slug}  === SHOULD BE /tags/{tag:name}
Route::get('/tags/{tag:name}',[TagController::class,'show'])->name("tags.show");


// Admin Dashboard
//Route::get('/admin',[DashboardController::class,'index'])->name("admin.index");

Route::prefix('admin')->name('admin.')->middleware(['auth','isadmin'])->group(function(){

    Route::get('/',[DashboardController::class,'index'])->name("index");


    Route::post('upload_tinymce_image',[TinyMCEController::class,'upload_tinymce_image'])->name('upload_tinymce_image');
    Route::resource('posts',AdminPostsController::class);
    Route::resource('categories',AdminCategoriesController::class);

});


require __DIR__.'/auth.php';

AdminCategoriesController.php

##
<?php

namespace App\Http\Controllers\AdminControllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Models\Category;

class AdminCategoriesController extends Controller
{


    public function index()
    {
        return view('dashboard.categories.index');
    }

    public function create()
    {
        return view('dashboard.categories.create');
    }

    public function store(Request $request)
    {

    }

    public function show(Category $category)
    {
        return view('dashboard.categories.show', [
            'category' => $category
        ]);
    }

    public function edit(Category $category)
    {
        return view('dashboard.categories.edit', [
            'category' => $category
        ]);
    }

    public function update(Request $request, Category $category)
    {

    }

    public function destroy(Category $category)
    {

    }
}

nav.blade.php

<!--sidebar wrapper -->
<div class="sidebar-wrapper" data-simplebar="true">
            <div class="sidebar-header">
                <div>
                    <img src="{{asset('assets/images/logo-icon.png')}}" class="logo-icon" alt="logo icon">
                </div>
                <div>
                    <h4 class="logo-text">MYBLOG</h4>
                </div>
                <div class="toggle-icon ms-auto"><i class='bx bx-arrow-to-left'></i>
                </div>
            </div>
            <!--navigation-->
            <ul class="metismenu" id="menu">
                <li>
                    <a href="" target="_blank">
                    <div class="parent-icon"><i class='bx bx-home-circle'></i></div>
                        <div class="menu-title">Dashboard</div>
                    </a>
                </li>


                <li>
                    <a href="javascript:;" class="has-arrow">
                        <div class="parent-icon"><i class='bx bx-message-square-edit'></i>
                        </div>
                        <div class="menu-title">Posts</div>
                    </a>

                    <ul>
                        <li> <a href="{{ route('admin.posts.index') }}"><i class="bx bx-right-arrow-alt"></i>All Posts</a>
                        </li>
                        <li> <a href="{{ route('admin.posts.create') }}"><i class="bx bx-right-arrow-alt"></i>Add New Post</a>
                        </li>

                    </ul>
                </li>


                <li>
                    <a href="" class="has-arrow">
                        <div class="parent-icon"><i class='bx bx-menu'></i>
                        </div>
                        <div class="menu-title">Categories</div>
                    </a>

                    <ul>
                        <li> <a href="{{ route('admin.categories.index') }}"><i class="bx bx-right-arrow-alt"></i>All Categories</a>
                        </li>
                        <li> <a href="{{ route('admin.categories.create') }}"><i class="bx bx-right-arrow-alt"></i>Add New Category</a>
                        </li>
                    </ul>

                </li>
            </ul>
            <!--end navigation-->
</div>
        <!--end sidebar wrapper -->

我開發了一個博客,並且有一個用於帖子的資源控制器,並且當我為類別創建另一個資源控制器時工作正常,給出了錯誤“路線[admin.categories.index] 未定義」。 。 只是我將路線放在儀表板中,如果我在 nav.blade.php 檔案中評論了類別的兩條路線,錯誤就會引發並消失

P粉983021177
P粉983021177

全部回覆(1)
P粉146080556

根據 Laravel 9 文件

Route::resources([
        'posts' => AdminPostsController::class,
        'categories'=> AdminCategoriesController::class
]);

然後我運行了 php artisan route:clear。 終於出現路線了。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板