Laravel路线:任何slug都会接受所有请求

时间:2017-03-05 18:19:37

标签: laravel routes request laravel-5.2

我有一条这样的路线。 $slug是一个变量,它与存储在数据库中的slug相匹配,可以动态地将页面添加到网站中。

#slug variable for different values of page slug....
Route::get('/{slug?}', array(
    'as' => 'page',
    'uses' => 'AbcController@renderPage'
));

但是,现在我想添加网站的管理员端,并希望路由以media-manager为前缀。

我的问题是,每当我调用文件中的另一条路由时,无论请求来自何处,上述路由都会接受请求调用并每次都调用renderPage方法。

这是我的中间件,我检查请求是否来自'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 个答案:

答案 0 :(得分:1)

在Laravel中使用第一个匹配路线。所以我猜你的slu the路线定义在其他路线之上(至少在媒体经理之上),对吗?

因此,一个简单的解决方案就是将slug路由定义放在路由文件的末尾。

另一种方法是利用路线的条件。有关详细信息,请阅读this或发表评论!

希望有所帮助!