2017-03-05 15 views
0

我有一個這樣的路線。 $slug是一個變量,它與存儲在數據庫中的段落相匹配,以動態地將頁面添加到網站。Laravel路線:任何slu takes需要所有的請求

#slug variable for different values of page slug.... 
Route::get('/{slug?}', array(
    'as' => 'page', 
    'uses' => '[email protected]' 
)); 

不過,現在我希望添加的網站的管理員側,並希望路線與media-manager前綴。

我的問題是,每當我打電話給文件中的另一條路由時,無論請求來自何處,上述路由都會接受請求調用並調用renderPage方法。

這是我的中間件,我檢查請求是否是從URL來像'media-manager/*',如果是的話我不想來檢查網站的語言,它重定向到媒體管理器的頁面。

private $openRoute = ['media-manager/login', 'media-manager/postLogin', 'media-manager/media']; 

public function handle($request, Closure $next) 
    { 

     foreach ($this->openRoute as $route) { 
      if ($request->is($route)) { 
       return $next($request); 
      } 
     } 


     // Make sure current locale exists. 
     $lang = $request->segment(1); 
     if(!isValidLang($lang)) { 
      $lang = getDefaultLang(); 
      $segments = $request->segments();    
      array_unshift($segments, $lang); 

      $newUrl = implode('/', $segments); 
      if (array_key_exists('QUERY_STRING', $_SERVER)) 
       $newUrl .= '?'.$_SERVER['QUERY_STRING']; 

      return $this->redirector->to($newUrl); 
     } 
     setLang($lang); 
     return $next($request); 
    } 

這是renderPage方法,其中每一次請求被重定向,不管是什麼。

public function renderPage($slug = '') 
    { 

     if ($slug == 'login') { 
      return view ('site.login'); 
     } 


     $page = Page::getBySlug($slug); 
     if(empty($page)){ 
      return URL::to ('/'); 
     } 

     if($slug == ''){//home page 
      $testimonial = DB::table('testimonial')->where('lang','=',$this->lang)->get(); 
      $client_logo = DB::table('client_logo')->get(); 
      return View::make('index', compact('data','page', 'testimonial', 'client_logo')); 

     }elseif($slug == 'services'){     
      return View::make('services', compact('page')); 

     }elseif($slug == 'portfolio'){ 
      $categories = PortfolioCategory::getAll(); 
      $portfolio = Portfolio::getAll(); 
      return View::make('portfolio', compact('page', 'categories', 'portfolio')); 

     }elseif($slug == 'oshara'){ 
      return View::make('oshara', compact('page')); 

     }elseif($slug == 'blog'){ 
      $limit = 8; 
      $pageNum = 1; 

      $offset = ($pageNum-1)*$limit; 
      $totalPosts = BlogPost::totalPosts(); 
      $totalPages = ceil($totalPosts/$limit); 
      $posts = BlogPost::getAll($offset, $limit); 
      $blog_posts = View::make('partials.blog_posts', compact('posts','pageNum','totalPages')); 
      return View::make('blog', compact('page', 'blog_posts', 'pageNum', 'totalPages')); 

     }elseif($slug == 'contact'){ 
      $budgets = Budget::getAll(); 
      return View::make('contact', compact('page', 'budgets')); 
     } 
    } 

這是我想用戶點擊登錄按鈕登錄頁面後致電控制器postLogin方法。

public function postLogin($request) { 
     # code... 
     //$request = $this->request; 
     $this->validate($request, [ 
      'email1' => 'required|email', 
      'password' => 'required|string' 
     ]); 
     if($user = User::whereEmail($request->email1)->first()) { 
      if(Hash::check($request['password'], $user->getAttributes()['password'])) { 

       if(!$user->getAttributes()['is_active']) { 
        return redirect('/media-manager/login')->withErrors('Your Account is not Activated Yet!'); 
       } else if($user->getAttributes()['is_deleted']) { 
        return redirect('/media-manager/login')->withErrors('Your Account is Banned!'); 
       } else { 
        # Success 
        $cookie = Cookie::make('user_id', $user->getAttributes()['id'], 864000); 
        //echo "hello"; 
        return view('site.media')->with('message', 'You have Successfully Logged In!')->withCookie($cookie); 
       } 
      } else { 
       return redirect('/media-manager/login')->withErrors('Your Login Information is Wrong!'); 
      } 
     } else { 
      return redirect('/media-manager/login')->withErrors('Your Login Information is Wrong!'); 
     } 
    } 

任何一個可以請建議我一些方法,這樣我可以在每次調用禁用renderPage方法,並有我的正常的路由完美的表現。

回答

1

在Laravel使用第一條匹配路線。所以我猜想你的slu route路線定義在別人之上(至少在媒體經理人之上),對吧?

所以一個簡單的解決方案是將slug路由定義放在路由文件的末尾。

另一種方法是利用條件的路線。欲瞭解更多信息,你可以閱讀this或發表評論!

希望幫助!

+0

我已經嘗試在每條路線下放置$ slug路線,但它不起作用。 – iammurtaza

+0

@iammurtaza您能否將您的路線定義添加到您的開場白? – Tek