I wish to route the same route to different controller base on user type.
Such as
if (Auth::check() && Auth::user()->is_admin) {
Route::get('/profile', 'AdminController@show');
} elseif (Auth::check() && Auth::user()->is_superadmin) {
Route::get('/profile', 'SuperAdminController@show');
}
But this doesn't work.
How can I make it works as what I want?
You can do this
Route::get('/profile', 'HomeController@profile'); // another route
Controller
public function profile() {
if (Auth::check() && Auth::user()->is_admin) {
$test = app('App\Http\Controllers\AdminController')->getshow();
}
elseif (Auth::check() && Auth::user()->is_superadmin) {
$test = app('App\Http\Controllers\SuperAdminController')->getshow();
// this must not return a view but it will return just the needed data , you can pass parameters like this `->getshow($param1,$param2)`
}
return View('profile')->with('data', $test);
}
But i think its better to use a trait
trait Show {
public function showadmin() {
.....
}
public function showuser() {
.....
}
}
Then
class HomeController extends Controller {
use Show;
}
Then you can do the same as the above but instead of
$test = app('App\Http\Controllers\AdminController')->getshow();// or the other one
use this
$this->showadmin();
$this->showuser(); // and use If statment ofc
okey you can do that by creating a route::group
your route group will be like that
route::group(['prefix'=>'yourPrefix','middleware'=>'yourMiddleware'],function(){
if (Auth::check() && Auth::user()->is_admin)
{
Route::get('profile', 'AdminController@show');
}
else
{
Route::get('profile', 'SuperAdminController@show');
}
});
I hope that will help you.