2016-06-17 1 views
0

사용자 유형에 따라 다른 컨트롤러베이스에 같은 경로를 라우팅하려고합니다.Laravel 5.2 조건에 따라 다른 컨트롤러 동작에 동일한 경로를 지정합니다.

이러한

if (Auth::check() && Auth::user()->is_admin) { 
     Route::get('/profile', '[email protected]'); 
    } elseif (Auth::check() && Auth::user()->is_superadmin) { 
     Route::get('/profile', '[email protected]'); 
    } 

하지만이 작동하지 않는 등.

내가 원하는대로 작동하게하려면 어떻게해야합니까?

+0

최고 관리자 컨트롤러와 관리 컨트롤러의 주요 차이점은 무엇입니까? 필자는 개인적으로 동일한 컨트롤러로 라우트하고 Auth 논리를 사용하여 컨트롤러의 어떤 메소드를 실행할 것인지 결정합니다. 일반적으로 경로 파일에 논리를 포함하는 것이 가장 좋은 방법은 아닙니다. –

+0

감사합니다. Rob, 그렇게하는 것이 좋지 않다는 것을 이해했습니다. Admin과 SuperAdmin은 내 시스템에서 대시 보드와 기능이 매우 다릅니다. 그래서 컨트롤러와 액션을 2 개로 분리하고 싶습니다. –

+0

여기에 귀하의 답변을 간다 [링크] (http://stackoverflow.com/questions/31368433/single-laravel-route-for-multiple-controllers) –

답변

3

당신은

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); 
      } 

는하지만 난 그것의 더 나은 형질 다음

trait Show { 

    public function showadmin() { 
    ..... 
    } 
    public function showuser() { 
    ..... 
    } 
} 

012을 사용하는 생각이

Route::get('/profile', '[email protected]'); // another route 

컨트롤러를 할 수

그런 다음 당신은 위와 동일한 작업을 수행 할 수 있습니다 대신

$test = app('App\Http\Controllers\AdminController')->getshow();// or the other one 

사용이

$this->showadmin(); 
$this->showuser(); // and use If statment ofc 
+0

둘 다 주셔서 감사합니다, 마침내 중간 남자로 HomeController와 함께 작동합니다. 그것은이 순간에 유일한 해결책 인 것 같습니다. 다시 한번 고마워. –

+0

편집에 대해 신경 쓰지 마라. 어쨌든 다행이다. –

1

좋아요 당신이 할 수있는 route::group

경로 그룹을 생성하여 될 것 그처럼

route::group(['prefix'=>'yourPrefix','middleware'=>'yourMiddleware'],function(){ 

     if (Auth::check() && Auth::user()->is_admin) 
     { 
      Route::get('profile', '[email protected]'); 
     } 
     else 
     { 
      Route::get('profile', '[email protected]'); 
     } 

    }); 

당신을 도울 수 있기를 바랍니다.

+0

나는 루트 파일에서 로직을 유지하는 것이 더 좋다고 생각한다 : 3 –

+0

나는 그의 질문에 대답했다. 나는 그가 정확히 알아야 할 것으로 대답했다. –

+0

yes 물론 모범 사례를 잊지 마십시오. 3 –