我最近开始使用新版本的Laravel 9以及惯性和Vue js 3,但是当我想从控制器调用函数时,我遇到了路由问题。正确的做法是什么?我给你留一个例子
用户控件
<?php
namespace AppHttpControllers;
use AppModelsUser;
use IlluminateHttpRequest;
use InertiaInertia;
class UserController extends Controller
{
public function index()
{
$users = User::all();
return Inertia::render('User',compact('users'));
}
public function create()
{
return Inertia::render('Create');
}
}
路线/web.php
<?php
use IlluminateFoundationApplication;
use IlluminateSupportFacadesRoute;
use InertiaInertia;
use AppHttpControllersUserController;
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');
create路由不起作用,正确的方法是什么?
->resource
将为控制器创建RESTFul路由;
执行以下命令获取所有可用路由:
php artisan route:list
所以你可能需要一个单一的动作,比如->get
Route::middleware(['auth:sanctum', config('jetstream.auth_session'), 'verified'])
->get('/create', [UserController::class, 'create'])
->name('create');
Route::get('create', [UserController::class, 'create']);
请尝试如下:我已经使用了Laravel Breez
带惯性的路由调用LINK
<div class="mb-2">
<Link :href="route('blogs.create')">
<BreezeButton>Add Blog</BreezeButton>
</Link>
</div>
web.php: Route
Route::get('/create', [BlogController::class, 'create'])->middleware(['auth', 'verified'])->name('blogs.create');
控制器中的函数调用
public function create()
{
return Inertia::render(
'Blogs/Create' // Your Create.vue will be in Blogs folder.
);
}