Habib Hassan
Habib Hassan

Reputation: 175

Domain Routing error Missing required parameters in Laravel

I have facing problem in my project. I create domain routing in laravel. But when I enter the login page route in shows error

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

Here is my route

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

    });
});

My login method code in Controller is

    <?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');
    }

}

And my blade file is

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

Also is there is any way to redirect if the user is not login to login page for subdomain.

I am using Laravel 5.7

My Error message

enter image description here

Thanks in advance

Upvotes: 3

Views: 2465

Answers (2)

Diogo Santo
Diogo Santo

Reputation: 789

For the last question you have, to redirect users when logged or not, you can make use of theAuth middleware`:

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

If you wish to be forceful, you can also implement a check at the top of your routes:

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

As for your login route, this is what I would do to help reduce the code and separate the logic in your method:

At the root folder of your laravel project, run the below command in your CMD:

php artisan make:request LoginRequest

This will create a new Class at app/Http/Requests

Within this class add your validation, so when the route and POST hists your method, the validation was already triggered:

LoginRequest class

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.',
        ...
    ];
}

Your login method at your Controller

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!
    ....
}

If working with more than one domain

Head to your config/app.php and add the other domains you need below the base_domain key:

return [

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

From this you will have to let Laravel know about your intentions. There are a few steps to follow and a few ways to better organize the code. I recommend this tutorial to help you setup subdomains in your laravel app!

Upvotes: 0

piscator
piscator

Reputation: 8719

Try to add the subdomain as a parameter to the route helper method, for example:

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

Upvotes: 1

Related Questions