zoukankan      html  css  js  c++  java
  • laravel4 中 Redirect::intended和Redirect::guest的关系及用法

    原文转自:http://forumsarchive.laravel.io/viewtopic.php?id=10653,有空的时候再翻译一下~

    This post will try to outline how to use the Redirect::intended and Redirect::guest methods.

    There seems to be a lot of confusion on the internet, along with a lot of custom solution in order to address this problem:
    Unauthenticated user accesses 'auth' filtered route
    Filter stores the intended URL and redirects to the 'login' route
    user tries to authenticate (post to login route)
    login route authenticates the user, checks the session for an intended route, then redirects to that if it exists, otherwise redirect to a fallback url.

    L4 introduced the Redirect::intended and Redirect::guest methods to solve this.
    The relevant source is located in vendorlaravelframeworksrcIlluminateRoutingRedirector.php.

    		/**
    	 * Create a new redirect response, while putting the current URL in the session.
    	 *
    	 * @param  string  $path
    	 * @param  int     $status
    	 * @param  array   $headers
    	 * @param  bool    $secure
    	 * @return IlluminateHttpRedirectResponse
    	 */
    	public function guest($path, $status = 302, $headers = array(), $secure = null)
    	{
    		$this->session->put('url.intended', $this->generator->full());
    
    		return $this->to($path, $status, $headers, $secure);
    	}
    
    	/**
    	 * Create a new redirect response to the previously intended location.
    	 *
    	 * @param  string  $default
    	 * @param  int     $status
    	 * @param  array   $headers
    	 * @param  bool    $secure
    	 * @return IlluminateHttpRedirectResponse
    	 */
    	public function intended($default, $status = 302, $headers = array(), $secure = null)
    	{
    		$path = $this->session->get('url.intended', $default);
    
    		$this->session->forget('url.intended');
    
    		return $this->to($path, $status, $headers, $secure);
    	}



    I believe it is used as follows (there may be some quirks, but its working for me in my project - feel free to contribute suggestions/changes)

    // Create a route protected by the 'auth' filter
    Route::get('protected', array('before'=>'auth', function()
    { 
            return View::make('protected');
     }));
    
    
    /*
    * the protected  'auth' filter - this runs before the protected route
    * if the user is not authenticated - perform a guest redirect (this stores the intended url is the session)
    */
    Route::filter('auth',function(){
            // the user hasnt logged in yet
    	if(Auth::guest()){
                    // redirect them to the login page with the guest method - this stores the intended URL
                    // IlluminateRoutingRedirector.php - Line 81: $this->session->put('url.intended', $this->generator->full());
    		return Redirect::guest('login');
    	}
    });
    
    /*
    * Our basic login function - point a form to POST here
    * if the authentication is successful, we will get the intended url from the session and redirect to it.
    * if there was no intended url (i.e. the user just navigated straight to the unportected login page), fallback to the specified route.
    * if authentication is unsuccessful, you can do whatever - here we redirect to the login page
    */
    Route::post('login', function()
    {
            //get the credentials from input
    	$creds = array('username'=>Input::get('username'),'password'=>Input::get('password'))
            
            // successful login attempt - redirect to the intended page, fallback to the '/' route if no intended page
    	if(Auth::attempt($creds,true)){
    		return Redirect::intended('/');
    	}
           // unsuccessful login - redirect somewhere
    	else{
    		return Redirect::to('login');
    	}
    });

    if anyone else wants to confirm this i will write up a post in the code samples maybe to outline it more clearly, or make a PR for the docs, as the Redirect::guest part is not in the documentation

  • 相关阅读:
    第二周例行报告
    洛谷 P3384 【模板】轻重链剖分
    洛谷 P3380 【模板】二逼平衡树(树套树)
    洛谷 P4568 [JLOI2011]飞行路线
    2018 ICPC Asia Nanjing Regional Preliminary L. Magical Girl Haze
    牛客 2020 牛客国庆集训派对 day8 G. Shuffle Cards
    洛谷 P3224 [HNOI2012]永无乡
    洛谷 P1486 [NOI2004]郁闷的出纳员
    洛谷 P3391 【模板】文艺平衡树
    洛谷 P3369 【模板】普通平衡树
  • 原文地址:https://www.cnblogs.com/ZJAJS/p/4432427.html
Copyright © 2011-2022 走看看