Thien
Thien

Reputation: 51

Laravel - The GET method is not supported for this route. Supported methods: POST

I am a newbie. I'm having problems with Laravel doing the Login function. After login will redirect to URL /admin/dashboard but I get an error

The GET method is not supported for this route. Supported methods: POST.

in web.php

Route::get('/login', [AdminController::class, 'index']);
Route::get('/logout', [AdminController::class, 'logout']);
Route::get('/dashboard', [AdminController::class, 'show_dashboard']);
Route::post('/admin/dashboard', [AdminController::class, 'dashboard']);

in controller

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use Session;
use Illuminate\Support\Facades\Redirect;
session_start();

class AdminController extends Controller
{
    public function index() {
        return view('admin.login');
    }

    public function show_dashboard() {
       return view('admin.dashboard.index');
    }

    public function dashboard(Request $request) {
        $email = $request->email;
        $password = md5($request->password);

        $result = DB::table('tbl_admin')->where('email', $email)->where('password', $password)->first();
        if ($result) {
            Session::put('id', $result->id);
            Session::put('name', $result->name);

            //return view('admin.dashboard.index');
            return redirect('/admin/dashboard');
        }
        else {
            Session::put('message', 'Email or password is incorrect.');
            return redirect('/login');
        }

    }

    public function logout() {
        Session::put('id', null);
        Session::put('name', null);
        return Redirect::to('/login');
    }
}

and in login.blade.php

<div class="log-w3">
        <div class="w3layouts-main">
            <h2>Sign In Now</h2>
                <form action="{{ url('/admin/dashboard')}}" method="POST">
                  {{ csrf_field() }}
                    <input type="text" class="ggg" name="email" placeholder="E-MAIL" required="">
                    <input type="password" class="ggg" name="password" placeholder="PASSWORD" required="">
                    <?php
                        $message = Session::get('message');
                        if($message) {
                            echo $message;
                            Session::put('message', null);
                        }
                    ?>
                    <span><input type="checkbox" />Remember Me</span>
                    <h6><a href="#">Forgot Password?</a></h6>
                        <div class="clearfix"></div>
                        <input type="submit" value="Sign In" name="submit">
                </form>
                <p>Don't Have an Account ?<a href="registration.html">Create an account</a></p>
        </div>

and version laravel

 "php": "^7.3|^8.0"

Can someone help me? Thanks.

Upvotes: 0

Views: 1211

Answers (1)

lagbox
lagbox

Reputation: 50481

You can change the redirect in the dashboard method to go to the GET route that returns the dashboard index instead of trying to redirect to a POST route. Change:

redirect('/admin/dashboard')

to:

redirect('dashboard')

Side notes:

This dashboard method on the Controller could probably be named something like login. Then show_dashboard could be dashboard.

You can remove the session_start() from the file. Laravel doesn't use PHP's sessions.

Upvotes: 1

Related Questions