Home > Mobile >  How are routes created in Laravel 9 using inertia?
How are routes created in Laravel 9 using inertia?

Time:07-20

I recently started using the new version of Laravel 9 together with Inertia and Vue js 3, but I'm having problems with the routes when I want to call a function from a controller. What would be the correct way to do it? I leave you an example

UserController

    <?php

   namespace App\Http\Controllers;

   use App\Models\User;
   use Illuminate\Http\Request;
   use Inertia\Inertia;

   class UserController extends Controller
   {

     public function index()
     {
       $users = User::all();
       return Inertia::render('User',compact('users'));
     }

     public function create()
     {    
    
       return Inertia::render('Create');
     }

   }

routes/web.php

<?php

use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;
use App\Http\Controllers\UserController;


Route::get('/', function () {
    return Inertia::render('Welcome', [
        'canLogin' => Route::has('login'),
        'canRegister' => Route::has('register'),
        'laravelVersion' => Application::VERSION,
        'phpVersion' => PHP_VERSION,
    ]);
});

Route::middleware([
    'auth:sanctum',
    config('jetstream.auth_session'),
    'verified',
])->group(function () {
    Route::get('/dashboard', function () {
        return Inertia::render('Dashboard');
    })->name('dashboard');
});

//Con el Middleware pide que el usuario este autentificado para ingresar a la ruta
//en render es el archivo vue a que llamamos, en este caso esta en resourrces, en js, pages

Route::middleware(['auth:sanctum',config('jetstream.auth_session'),'verified'])->resource('/user',UserController::class);

//this not work
Route::middleware(['auth:sanctum',config('jetstream.auth_session'),'verified'])->resource('/create',[UserController::class,'create'])->name('create');

the create route does not work, what would be the correct way?

CodePudding user response:

->resource will create RESTFul routes for a controller;

Run following command to get all the available routes:

php artisan route:list

So instead of a resource you might need a single action like ->get

Route::middleware(['auth:sanctum', config('jetstream.auth_session'), 'verified'])
->get('/create', [UserController::class, 'create'])
->name('create');

CodePudding user response:

Route::get('create', [UserController::class, 'create']);

  • Related