Laravel Redirect :: to()不起作用

时间:2014-04-23 19:11:32

标签: php redirect laravel laravel-4

我正在为我的项目创建一个twitter登录功能,oauth步骤,用户已授予我的应用程序使用其数据的权限,将用户返回到/ twitter-auth路由,此路由依次启动此方法:

public function auth() {
    /* Oauth token */ 
    $token = Input::get('oauth_token');

    /* Verifier token */
    $verifier = Input::get('oauth_verifier');

    /* Request access token */
    $accessToken = Twitter::oAuthAccessToken($token, $verifier);


    /* Set the session variables from the acccess token above */
    Session::set('user_id', $accessToken['user_id']);
    Session::set('username', $accessToken['screen_name']);
    Session::set('oauth_token', $accessToken['oauth_token']);
    Session::set('oauth_token_secret', $accessToken['oauth_token_secret']);

    /* Determine if the user already exists in the database, if he/she does, then
    only update the user, otherwise, store a new user. Also pass an instance of the 
    accessToken as flash data in both instances. */
    if( User::where('twitter_id', $accessToken['user_id'])->first() == null )
    {

        $newUser = array(
            'username' => $accessToken['screen_name'],
            'oauth_token' => $accessToken['oauth_token'],
            'oauth_token_secret' => $accessToken['oauth_token_secret'],
            'twitter_id' => $accessToken['user_id']
        );

        User::create( $newUser );
        return Redirect::to('/');

    }

    else
    {
        $userToUpdate = User::where('twitter_id', Session::get('user_id'))->first();

        $userToUpdate->username = $accessToken['screen_name'];
        $userToUpdate->oauth_token = $accessToken['oauth_token'];
        $userToUpdate->oauth_token_secret = $accessToken['oauth_token_secret'];
        $userToUpdate->twitter_id = $accessToken['user_id'];

        $userToUpdate->save();
        return Redirect::to('/');

    }


}

根据需要保存/更新用户,但不会将用户重定向到主页。这种情况发生在IF语句内部和外部的重定向代码中。我想知道是否有人可以给我任何关于为什么重定向不起作用的线索?

3 个答案:

答案 0 :(得分:3)

您错过了return

在这种情况下你的函数auth()正在返回Redirect对象,但调用你的auth()函数的函数是将结果返回给控制器吗?

答案 1 :(得分:2)

请确保在控制器中返回来自auth()函数的Redirect类。 刚刚测试过你的代码并且有效:

让我们说你有一个UserController:

routes.php文件

Route::get('twitter-auth',array('as'=>'twitter-auth', 'uses'=>'UserController@twitterAuth'));

UserController中    用户模型类只是通过依赖注入传递,以测试这部分。

<?php

class UserController extends BaseController {

    public function __construct(User $u){
        $this->user = $u;
    }


    public function twitterAuth(){

        return $this->user->auth();



    }

}

用户模型: 我不得不修改一些代码以适应我的设置

public function auth(){

    /* Oauth token */ 
    $token = Input::get('oauth_token');

    /* Verifier token */
    $verifier = Input::get('oauth_verifier');

    /* Request access token */
    //$accessToken = Twitter::oAuthAccessToken($token, $verifier);
    //emulate the request of access Token
    $accessToken = [
        'user_id'=>'11',
        'screen_name'=>'fewfewfew',
        'oauth_token'=>'12312321',
        'oauth_token_secret'=>'12312232323'
    ];

    /* Set the session variables from the acccess token above */
    Session::set('user_id', $accessToken['user_id']);
    Session::set('username', $accessToken['screen_name']);
    Session::set('oauth_token', $accessToken['oauth_token']);
    Session::set('oauth_token_secret', $accessToken['oauth_token_secret']);


    /* Determine if the user already exists in the database, if he/she does, then
    only update the user, otherwise, store a new user. Also pass an instance of the 
    accessToken as flash data in both instances. */
    if( User::where('twitter_id', $accessToken['user_id'])->first() == null )
    {

        $newUser = array(
            'username' => $accessToken['screen_name'],
            'oauth_token' => $accessToken['oauth_token'],
            'oauth_token_secret' => $accessToken['oauth_token_secret'],
            'twitter_id' => $accessToken['user_id']
        );

        User::create( $newUser );
        return Redirect::to('/');

    }

    else
    {
        $userToUpdate = User::where('twitter_id', Session::get('user_id'))->first();

        $userToUpdate->username = $accessToken['screen_name'];
        $userToUpdate->oauth_token = $accessToken['oauth_token'];
        $userToUpdate->oauth_token_secret = $accessToken['oauth_token_secret'];
        $userToUpdate->twitter_id = $accessToken['user_id'];

        $userToUpdate->save();
        return Redirect::to('/');

    }


}

如果这是您想要的,请告诉我

答案 2 :(得分:2)

只能从路由,控制器操作和过滤器返回重定向来执行它。否则你必须调用send()

重定向::至(&#39;登录&#39;) - &gt;置于();