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

  • 相关阅读:
    QuantLib 金融计算
    【翻译】《理解收益率曲线》系列
    QuantLib 金融计算——C++ 代码改写成 Python 程序的一些经验
    可转债研报阅读笔记
    SWIG 3 中文手册——13. 约定
    SWIG 3 中文手册——12. 自定义功能
    SWIG 3 中文手册——11. 类型映射
    【翻译】Quant 应该怎样写博客?
    QuantLib 金融计算——案例之普通利率互换分析(2)
    仿射期限结构模型:理论与实现——实现部分
  • 原文地址:https://www.cnblogs.com/ZJAJS/p/4432427.html
Copyright © 2011-2022 走看看