Abaij
Abaij

Reputation: 873

Route group prefix not working in Laravel 8

I was trying to make a group routing with a prefix in Laravel 8. But when I tested it in http://localhost/mysite/admin/test/, it always throws error 404.

Here is the code in web.php:

Route::prefix('/admin', function() {
    Route::get('/test', [Admin\LoginController::class, 'index']);
});

I created a controller in app/Http/Controller/Admin/ as the controller is inside Admin folder.

Here is the code in LoginController:

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    public function __construct()
    {
        //
    }

    public function index()
    {
        echo "Please login";
    }
}

Can anybody show me what I am doing wrong to get it working?

Upvotes: 0

Views: 8491

Answers (3)

Trupti Hala
Trupti Hala

Reputation: 1

  1. Try this

    use App\Http\Controllers\Admin\LoginController; 
    
    Route::prefix('admin')->group(function () {
         Route::get('test', ['LoginController::class, index'])->name('test'); });
    

Upvotes: 0

Gonzalo F S
Gonzalo F S

Reputation: 408

You have to group the routes as stated in the documentation like:

Route::prefix('admin')->group(function () {
    Route::get('/test', function () {
        // Matches The "/admin/users" URL
    });
});

In your case it would be:

use App\Http\Controllers\Admin\LoginController;

Route::prefix('admin')->group(function () {
    Route::get('/test', [LoginController::class, 'index']);
});

Upvotes: 4

NoOorZ24
NoOorZ24

Reputation: 3222

I think it should be 'admin' not '/admin'. That slash makes it:

http://localhost/mysite//admin/test

=>

http://localhost/admin/test

You can check all your routes using: php artisan route:list

Upvotes: 0

Related Questions