我正在try 实现一个登录系统,只有在url中有get参数时,用户才会被重定向回该系统,否则它将被重定向到配置文件页面.

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

/login

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

但是如果uri有get参数,比如

/login?r=articles

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

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

routes.php

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

UserController.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

<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

谢谢大家的回复,事实是我仍然在了解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.

signin.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

// 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.

推荐答案

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

request()->r

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

request()->filled('r')来确定它是否存在于URI中,并且它的值不为空.

Laravel相关问答推荐

警告:构建Reaction App(VITE)后无法解码下载的字体警告

laravel Eloquent 模型更新事件未触发

在 laravel 中动态更改时区

Laravel 5.4 中的自定义助手类

Eloquent: hasNot 带参数

Lumen和 MongoDB?

Laravel 5 make:控制器在应用程序文件夹而不是控制器文件夹中创建控制器

Laravel - 如何恢复本地存储符号链接并刷新

Cookie::forget 不工作 laravel 5.1

调用未定义的方法 Illuminate\Foundation\Application::bindShared()

无法打开流:没有这样的文件或目录(Laravel)

如何仅在Blade模板存在时才包含它?

如何在不使用视图的情况下使用 Laravel 4 发送Electron邮件?

Select,where JSON 包含数组

使用 Nginx 设置 Laravel

Eloquent的 attach/detach/sync 触发任何事件?

在哪里放置 Blade::extend

Laravel Route 模型与关系绑定

使用迁移更改表 Laravel 5

laravel中的动态网址?