Laravel 5 如何使用控制器的登录方法从 url 获取参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38107085/
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-08 17:26:27  来源:igfitidea点击:

Laravel 5 how to use get parameter from url with controller's sign in method

laravelcontrollergetlaravel-5.2

提问by Bruno

I'm trying to implement a login system where the user will be redirect back only if there is a get parameter in the url, else it will be redirect to the profile page.

我正在尝试实现一个登录系统,只有当 url 中有 get 参数时,用户才会被重定向回,否则它将被重定向到个人资料页面。

So, if the uri is something like this (no get parameter)

所以,如果uri是这样的(没有get参数)

/login

if success, the user will be redirect to the profile page.

如果成功,用户将被重定向到个人资料页面。

But if the uri has the get parameter like for example

但是,如果 uri 具有 get 参数,例如

/login?r=articles

the user will be redirected to the articles page instead of the default route to the profile page.

用户将被重定向到文章页面,而不是默认路由到个人资料页面。

Question is, in the controller, how can do this, if possible, or how can I check for the get parameter?

问题是,在控制器中,如果可能,如何执行此操作,或者如何检查 get 参数?

routes.php

路由文件

// Signin
Route::post('/account/signin', [
    'uses' => 'UserController@postSignIn',
    'as' => 'user.signin',
]);

UserController.php

用户控制器.php

// Signin
public function postSignIn(Request $request)
{
    $this->validate($request, [
        'login-email' => 'required|email',
        'login-password' => 'required'
    ]);

    if ( Auth::attempt(['email' => $request['login-email'], 'password' => $request['login-password']]) )
    {

        // Tried this, isn't working... (maybe something's missing ??)
            $redirect = $request->input('r');
            if ($redirect) {
                return redirect()->route($redirect);
            }
        // -->

        return redirect()->route('user.account');
    }
    return redirect()->back();
}

signin.blade.php

登录名.blade.php

<form role="form" action="{{ route('user.signin') }}" method="post" class="login-form" name="login">

    <div class="form-group {{ $errors->has('login-email') ? 'has-error' : '' }}">
        <label class="sr-only" for="email">Email</label>
        <input type="text" name="login-email" value="{{ Request::old('login-email') }}" placeholder="Email..." class="form-username form-control" id="form-username">
    </div>

    <div class="form-group {{ $errors->has('login-password') ? 'has-error' : '' }}">
        <label class="sr-only" for="password">Password</label>
        <input type="password" name="login-password" value="{{ Request::old('login-password') }}" placeholder="Password..." class="form-password form-control" id="form-password">
    </div>

    <div class="form-group">
        <input type="checkbox" name="remember" value="{{ Request::old('remember') }}" id="remember">
        Remember
    </div>

    <button type="submit" class="btn">Sign in!</button>

    <input type="hidden" name="_token" value="{{ Session::token() }}">
</form>

Thanks.

谢谢。

Updated

更新

Thank you all for your replies, the fact is that I'm still getting to know Laravel and that's probably why I can't implement it right the solutions that you guys shared.

感谢大家的回复,事实是我仍在了解 Laravel,这可能就是为什么我无法正确实施你们分享的解决方案。

So this said, I got it working by creating a conditional hidden field that holds the query value and this way once the user submits the form, it will be passed with the rest of the $response arguments.

所以这就是说,我通过创建一个保存查询值的条件隐藏字段来让它工作,这样一旦用户提交表单,它将与其余的 $response 参数一起传递。

signin.blade.php

登录名.blade.php

<form role="form" action="{{ route('user.signin') }}" method="post" class="login-form" name="login">

    <div class="form-group {{ $errors->has('login-email') ? 'has-error' : '' }}">
        <label class="sr-only" for="email">Email</label>
        <input type="text" name="login-email" value="{{ Request::old('login-email') }}" placeholder="Email..." class="form-username form-control" id="form-username">
    </div>

    <div class="form-group {{ $errors->has('login-password') ? 'has-error' : '' }}">
        <label class="sr-only" for="password">Password</label>
        <input type="password" name="login-password" value="{{ Request::old('login-password') }}" placeholder="Password..." class="form-password form-control" id="form-password">
    </div>

    <div class="form-group">
        <input type="checkbox" name="remember" value="{{ Request::old('remember') }}" id="remember">
        Remember
    </div>

    <button type="submit" class="btn">Sign in!</button>

    <input type="hidden" name="_token" value="{{ Session::token() }}">
    <!-- Verify condition -->
    @if(isset($_GET['referer']))
        <input type="hidden" name="referer" value="{{ $_GET['referer'] }}">
    @endif
</form>

UserController.php

用户控制器.php

// Signin
public function postSignIn(Request $request)
{
    $this->validate($request, [
        'login-email' => 'required|email',
        'login-password' => 'required'
    ]);

    if ( Auth::attempt(['email' => $request['login-email'], 'password' => $request['login-password']]) )
    {

        // Check for the new argument 'referer'
        if (isset($request->referer)) {
            return redirect()->route($request->referer);
        }
        // -->

        return redirect()->route('user.account');
    }
    return redirect()->back();
}

Like so, it works.

像这样,它的工作原理。

Don't know if it's a viable and secure way to do it in Laravel 5, but it is working.

不知道在 Laravel 5 中这是否是一种可行且安全的方法,但它正在起作用。

回答by user2094178

When you have an URI such as login?r=articles, you can retrieve articleslike this:

当你有一个 URI 时login?r=articles,你可以articles像这样检索:

request()->r

You can also use request()->has('r')to determine if it's present in the URI.

您还可以使用request()->has('r')它来确定它是否存在于 URI 中。

And request()->filled('r')to find out if it's present in the URI and its value is not empty.

request()->filled('r')找出它是否存在于 URI 中并且其值不为空。

回答by Laravel User

// only query

// 只查询

$query_array = $request->query();

or

或者

$query = $request->query('r');

// Without Query String...

// 没有查询字符串...

$url = $request->url();

// With Query String...

// 使用查询字符串...

$url = $request->fullUrl();

回答by msonowal

If you are using Laravel then use their helper which works just out of the box, i.e. if your route or url has a auth middlewere and user is not logged in then it goes to login and in your postSign or inside attempt just

如果您使用的是 Laravel,则使用他们开箱即用的帮助程序,即如果您的路由或 url 有一个 auth 中间件并且用户未登录,则它会登录并在您的 postSign 或内部尝试中

 return redirect()->intended('home'); //home is the fallback if no intended url is provide

UserController

用户控制器

public function postSignIn(Request $request){
$this->validate($request, [
    'login-email' => 'required|email',
    'login-password' => 'required'
]);

if ( Auth::attempt(['email' => $request['login-email'], 'password' => $request['login-password']]) )
{

    return redirect()->intended('user.account);
}
return redirect()->back();

}

}

回答by Ali Hassan

first use this

首先使用这个

use Illuminate\Support\Facades\Input;

then you can get any data from your url

然后你可以从你的网址中获取任何数据

$name=Input::get('term', false); //term is the parameter name we get from URL