域路由错误Laravel中缺少必需的参数

时间:2018-12-07 11:56:44

标签: php laravel routing routes subdomain

我的项目遇到了问题。我在laravel中创建域路由。但是当我输入登录页面中的路线时显示错误

Missing required parameters for [Route: admin.login.submit] [URI: login]. 

这是我的路线

Route::domain(env('APP_DOMAIN_URL'))->group(function () {
    Route::any('/', function() {
        return 'Site is under development';
    });
});

Route::domain('{subdomain}.' . env('APP_DOMAIN_URL'))->group(function () {

    Route::group(['namespace' => 'backend'], function() {
        Route::get('login', 'AdminLoginController@showLogin')->name('admin.login');
        Route::post('login', 'AdminLoginController@login')->name('admin.login.submit');
        Route::get('/', 'AdminController@dashboard')->name('admin.dashboard');
        Route::get('logout', 'AdminLoginController@logout')->name('admin.logout');

    });
});

我在Controller中的登录方法代码是

    <?php

namespace App\Http\Controllers\backend;

use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AdminLoginController extends Controller {

    public function __construct() {
        $this->middleware('guest:admin', ['except' => ['logout']]);
    }

    public function showLogin() {
        return view('backend.login');
    }

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

        // Attempt to log the user in
        if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password, 'status' => 1], $request->remember)) {
            // if successful, then redirect to their intended location
            return redirect()->intended(route('admin.dashboard'));
        }

        // if unsuccessful, then redirect back to the login with the form data
        $errors = ['email' => trans('auth.failed')];
        return redirect()->back()->withInput($request->only('email', 'remember'))->withErrors($errors);
    }

    public function logout() {        
        return redirect()->route('admin.dashboard');
    }

}

我的刀片文件是

<div class="login-box-body">
    <p class="login-box-msg">Sign in to start your session</p>
    @include('backend.include.login_error')
    @include('backend.include.flashMessage')
    <form action="{{ route('admin.login.submit') }}" method="post">
        {{ csrf_field() }}
        <div class="form-group has-feedback">
            <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus placeholder="Email">
            <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
        </div>
        <div class="form-group has-feedback">
            <input id="password" type="password" class="form-control" name="password" required placeholder="Password">
            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
        </div>
        <div class="row">
            <div class="col-xs-8">
                <div class="checkbox icheck">
                    <label>
                        <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : ''}}> Remember Me
                    </label>
                </div>
            </div>
            <!-- /.col -->
            <div class="col-xs-4">
                <button type="submit" class="btn btn-success btn-block btn-flat">Sign In</button>
            </div>
            <!-- /.col -->
        </div>
    </form>

</div>

如果用户未登录到子域的登录页面,则还有任何方法可以重定向。

我正在使用Laravel 5.7

我的错误消息

enter image description here

预先感谢

2 个答案:

答案 0 :(得分:1)

尝试将子域作为参数添加到路由帮助器方法中,例如:

<form 
    action="{{ route('admin.login.submit', ['subdomain' => 'admin']) }}" 
    method="post">

答案 1 :(得分:0)

对于最后一个问题,to redirect users when logged or not, you can make use of the验证中间件`:

Route::group(['middleware' => ['auth']], function() {
    //Only Auth routes that you wish to allow, if user is authenticated
});

如果您想变得有力,也可以在路线顶部进行检查:

if (Auth::guest()) {
   return redirect('login');
}

关于您的登录路径,这是我的工作,以帮助减少代码并分离method中的逻辑:

在laravel项目的根文件夹中,在CMD中运行以下命令:

php artisan make:request LoginRequest

这将在Class处创建一个新的app/Http/Requests

在该类中添加您的验证,因此当路由和POST提示您的方法时,验证已触发:

LoginRequest类

public function autorize()
{
    if (Auth::check()) {
        return false; //User already logged in, no point in going through this again
    }
    return true; //It is a guest so lets approve it
}

public function rules()
{
    return [
        'email'    => 'required|email|users:unique',
        'password' => 'required'
    ];
}

public function messages()
{
    return [
        'email.required' => 'You need to add an email to login.',
        'email.email'    => 'The information provided does not seem to be an email.',
        ...
    ];
}

您在控制器上的登录方式

public function login(LoginRequest $request)
{
    // You can remove the first lines that do the validation as this is triggered before arriving to the method now!
    ....
}

如果使用多个域<​​/ strong>

转到您的config/app.php,然后在base_domain键下方添加您需要的其他域:

return [

   'base_domain' => env('APP_BASE_DOMAIN'),
    ...
];

由此,您必须让Laravel知道您的意图。有一些步骤可以遵循,还有一些方法可以更好地组织代码。我建议this tutorial来帮助您在laravel应用中设置子域!