recherche

Maison  >  Questions et réponses  >  le corps du texte

J'ai rencontré une erreur de route non définie lors de l'exécution de la commande "make:controller nom_contrôleur --resource".

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

Je développe un blog et j'ai un contrôleur de ressources pour les articles et lorsque je crée un autre contrôleur de ressources pour les catégories, cela fonctionne bien, donnant l'erreur "La route [admin.categories.index] n'est pas définie". . Juste je mets les routes dans le tableau de bord et si je commente les deux routes pour la catégorie dans le fichier nav.blade.php l'erreur est levée et disparaît

P粉983021177P粉983021177267 Il y a quelques jours456

répondre à tous(1)je répondrai

  • P粉146080556

    P粉1460805562024-04-03 16:59:25

    Selon la documentation de Laravel 9

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

    Ensuite, j'ai exécuté php artisan route:clear. Finalement, l'itinéraire est apparu.

    répondre
    0
  • Annulerrépondre