Laravel 8 多条路线相同 Url
Laravel 8 Multiple Routes With The Same Url
美好的一天
我是 Laravel 的新手,我想创建一个拥有多个用户(学生、主管、HOD)的网站。我想知道是否有办法让所有用户拥有相同的 URL。以下是我用来尝试实现此目的的方法:
web.php
// Routes For Students
Route::group(['middleware' => ['auth', 'role:student']], function() {
Route::get('/home', 'App\Http\Controllers\StudentController@index')->name('student');
Route::get('/proposal', 'App\Http\Controllers\StudentController@proposal')->name('proposal');
Route::get('/thesis', 'App\Http\Controllers\StudentController@thesis')->name('thesis');
});
// Routes For Supervisors
Route::group(['middleware' => ['auth', 'role:supervisor']], function() {
Route::get('/home', 'App\Http\Controllers\supervisorController@index')->name('supervisor');
Route::get('/proposal', 'App\Http\Controllers\supervisorController@proposal')->name('proposal');
Route::get('/thesis', 'App\Http\Controllers\supervisorController@thesis')->name('thesis');
});
// Routes For HOD's
Route::group(['middleware' => ['auth', 'role:hod']], function() {
Route::get('/home', 'App\Http\Controllers\hodController@index')->name('hod');
Route::get('/proposal', 'App\Http\Controllers\hodController@proposal')->name('proposal');
Route::get('/thesis', 'App\Http\Controllers\hodController@thesis')->name('thesis');
});
但是我注意到你不能有相同 URL 的路由,但我希望所有类型的用户在他们各自的主页、提案和论文页面上都具有相同的 URL。这可能吗?
如果您需要更多代码或更好的解释,请告诉我。
我建议只为所有这些路由添加一个前缀。这样你就不会得到任何重复的 uris。
如果你真的不想这样做,我还有另一种可能性,但你将无法保留路由名称。
基本上,使用可调用控制器重新路由到正确的 controller/action。
php artisan make:controller -i
Route::group(['middleware' => ['auth']], function () {
Route::get('/home', 'App\Http\Controllers\MyInvokableController')->name('home');
Route::get('/proposal', 'App\Http\Controllers\MyInvokableController')->name('proposal');
Route::get('/thesis', 'App\Http\Controllers\MyInvokableController')->name('thesis');
});
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MyInvokableController extends Controller
{
private $lookupArray = [
'home' => 'index',
'proposal' => 'proposal',
'thesis' => 'thesis',
];
public function __invoke(Request $request)
{
$role = ;// get your user's role
$urlSegment = $request->segment(count($request->segments()) - 1); // 'home', 'proposal' or 'thesis'
if ($role === 'student') {
return app(StudentController::class)->{$this->lookupArray[$urlSegment]}($request);
} elseif ($role === 'supervisor') {
return app(supervisorController::class)->{$this->lookupArray[$urlSegment]}($request);
} elseif ($role === 'hod') {
return app(hodController::class)->{$this->lookupArray[$urlSegment]}($request);
} else {
throw new \Exception("Unexpected role $role.");
}
}
}
如果您需要保持相同的路线,您可以按照@IGP 的回答或只检查 blade 本身的用户角色。
// welcome.blade.php
@role('student')
<h1>Student welcome page</h1>
@endrole
@role('supervisor')
<h1>Supervisor welcome page</h1>
@endrole
@role('hod')
<h1>Hod welcome page</h1>
@endrole
我认为你最好在控制器上处理逻辑而不是角色中间件。是否有主管担任学生“角色”?不,那是因为 Supervisor 在这里不是角色,而是用户类型。所以在这里你想要相同的URL,这是我更喜欢的:
Route::group(['middleware' => ['auth']], function() {
Route::get('/home', 'App\Http\Controllers\Controller@index')->name('home');
Route::get('/proposal', 'App\Http\Controllers\Controller@proposal')->name('proposal');
Route::get('/thesis', 'App\Http\Controllers\Controller@thesis')->name('thesis');
});
在幕后处理用户类型。
public function index(Request $request)
{
// handle the request, determine the user type, populate the proper view, etc.
}
现在谈谈角色,
Middleware can also receive additional parameters. For example, if your application needs to verify that the authenticated user has a given "role" before performing a given action. Laravel 8 Documentation - Middleware
举个例子,您可以在您的应用程序中设置“演示者”角色,该角色具有唯一的 URL 并且允许学生和主管访问它们。
仅仅使用中间件并不意味着它就是一个有效的解决方案。正如文档所说:
Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering your application. Laravel Documentation
美好的一天
我是 Laravel 的新手,我想创建一个拥有多个用户(学生、主管、HOD)的网站。我想知道是否有办法让所有用户拥有相同的 URL。以下是我用来尝试实现此目的的方法:
web.php
// Routes For Students
Route::group(['middleware' => ['auth', 'role:student']], function() {
Route::get('/home', 'App\Http\Controllers\StudentController@index')->name('student');
Route::get('/proposal', 'App\Http\Controllers\StudentController@proposal')->name('proposal');
Route::get('/thesis', 'App\Http\Controllers\StudentController@thesis')->name('thesis');
});
// Routes For Supervisors
Route::group(['middleware' => ['auth', 'role:supervisor']], function() {
Route::get('/home', 'App\Http\Controllers\supervisorController@index')->name('supervisor');
Route::get('/proposal', 'App\Http\Controllers\supervisorController@proposal')->name('proposal');
Route::get('/thesis', 'App\Http\Controllers\supervisorController@thesis')->name('thesis');
});
// Routes For HOD's
Route::group(['middleware' => ['auth', 'role:hod']], function() {
Route::get('/home', 'App\Http\Controllers\hodController@index')->name('hod');
Route::get('/proposal', 'App\Http\Controllers\hodController@proposal')->name('proposal');
Route::get('/thesis', 'App\Http\Controllers\hodController@thesis')->name('thesis');
});
但是我注意到你不能有相同 URL 的路由,但我希望所有类型的用户在他们各自的主页、提案和论文页面上都具有相同的 URL。这可能吗?
如果您需要更多代码或更好的解释,请告诉我。
我建议只为所有这些路由添加一个前缀。这样你就不会得到任何重复的 uris。
如果你真的不想这样做,我还有另一种可能性,但你将无法保留路由名称。
基本上,使用可调用控制器重新路由到正确的 controller/action。
php artisan make:controller -i
Route::group(['middleware' => ['auth']], function () {
Route::get('/home', 'App\Http\Controllers\MyInvokableController')->name('home');
Route::get('/proposal', 'App\Http\Controllers\MyInvokableController')->name('proposal');
Route::get('/thesis', 'App\Http\Controllers\MyInvokableController')->name('thesis');
});
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MyInvokableController extends Controller
{
private $lookupArray = [
'home' => 'index',
'proposal' => 'proposal',
'thesis' => 'thesis',
];
public function __invoke(Request $request)
{
$role = ;// get your user's role
$urlSegment = $request->segment(count($request->segments()) - 1); // 'home', 'proposal' or 'thesis'
if ($role === 'student') {
return app(StudentController::class)->{$this->lookupArray[$urlSegment]}($request);
} elseif ($role === 'supervisor') {
return app(supervisorController::class)->{$this->lookupArray[$urlSegment]}($request);
} elseif ($role === 'hod') {
return app(hodController::class)->{$this->lookupArray[$urlSegment]}($request);
} else {
throw new \Exception("Unexpected role $role.");
}
}
}
如果您需要保持相同的路线,您可以按照@IGP 的回答或只检查 blade 本身的用户角色。
// welcome.blade.php
@role('student')
<h1>Student welcome page</h1>
@endrole
@role('supervisor')
<h1>Supervisor welcome page</h1>
@endrole
@role('hod')
<h1>Hod welcome page</h1>
@endrole
我认为你最好在控制器上处理逻辑而不是角色中间件。是否有主管担任学生“角色”?不,那是因为 Supervisor 在这里不是角色,而是用户类型。所以在这里你想要相同的URL,这是我更喜欢的:
Route::group(['middleware' => ['auth']], function() {
Route::get('/home', 'App\Http\Controllers\Controller@index')->name('home');
Route::get('/proposal', 'App\Http\Controllers\Controller@proposal')->name('proposal');
Route::get('/thesis', 'App\Http\Controllers\Controller@thesis')->name('thesis');
});
在幕后处理用户类型。
public function index(Request $request)
{
// handle the request, determine the user type, populate the proper view, etc.
}
现在谈谈角色,
Middleware can also receive additional parameters. For example, if your application needs to verify that the authenticated user has a given "role" before performing a given action. Laravel 8 Documentation - Middleware
举个例子,您可以在您的应用程序中设置“演示者”角色,该角色具有唯一的 URL 并且允许学生和主管访问它们。
仅仅使用中间件并不意味着它就是一个有效的解决方案。正如文档所说:
Middleware provide a convenient mechanism for inspecting and filtering HTTP requests entering your application. Laravel Documentation