Laravel Redirect::to() 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23253184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:24:44  来源:igfitidea点击:

Laravel Redirect::to() not working

phpredirectlaravellaravel-4

提问by Carwyn Stephen

I am creating a twitter log in feature for my project, the oauth step where the user has granted permission for my app to use their data returns the user to the /twitter-auth route, this route in turn initiates this method:

我正在为我的项目创建一个 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('/');

    }


}

The user is saved/updated as necessary, but the user is not redirected to the home page. This happens with the redirect code both inside and outside of the IF statement. I was wondering if anyone could give me any clues as to why the redirect isn't working?

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

回答by Mahmoud Zalt

You are missing a return

你缺少一个 return

your function in this case auth()is returning the Redirectobject but is the function calling your auth()function is returning the result back to the controller?

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

回答by Haisum Usman

Returning a Redirect to execute it is only possible from routes, controller actions and filters. Otherwise you have to call send()

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

Redirect::to('login')->send();

重定向::到('登录')->发送();

回答by Paul Bele

Please make sure that in your controller, you return the Redirect class that is from auth() function. Just tested your code and works :

请确保在您的控制器中,您返回来自 auth() 函数的 Redirect 类。刚刚测试了您的代码并工作:

let's say you have a UserController :

假设您有一个 UserController :

routes.php

路由文件

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

UserController the user model class is just passed by dependency injection, to test this part also.

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

<?php

class UserController extends BaseController {

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


    public function twitterAuth(){

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



    }

}

User model : I had to modify the code a little to fit my setup also

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

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('/');

    }


}

Let me know if this is what you wanted

让我知道这是否是你想要的