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

  • 相关阅读:
    同一页面的不同Iframe获取数据
    同一页面的两个Iframe获取数据
    关于从SVN检出项目后,项目名称还是之前修改之前或者项目名称不对问题
    <fieldset>标签
    利用js动态创建<style>
    找换硬币问题 与 0-1背包问题区别
    某种 找换硬币问题的贪心算法的正确性证明
    部分背包问题的贪心算法正确性证明
    从 活动选择问题 看动态规划和贪心算法的区别与联系
    求解两个字符串的最长公共子序列
  • 原文地址:https://www.cnblogs.com/ZJAJS/p/4432427.html
Copyright © 2011-2022 走看看