Mr.Rice Mr.Rice - 8 months ago 53
PHP Question

Laravel 5.2 assigning same route to different controller action by conditions

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


    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() {


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->showuser(); // and use If statment ofc