Home > Software design >  Laravel 9 routes not working with custom method
Laravel 9 routes not working with custom method

Time:11-07

I have a large route file. All of my routes are working except for shop.calendar(returns 404). I'm truly baffled!

Here is my route file

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\LeadController;
use App\Http\Controllers\NoteController;
use App\Http\Controllers\ClientController;
use App\Http\Controllers\DeviceController;
use App\Http\Controllers\IntakeController;
use App\Http\Controllers\PaymentController;
use App\Http\Controllers\VehicleController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\AppointmentController;
use App\Http\Controllers\ShopController;

Route::redirect('/', 'login');

Route::group(['middleware' => 'auth'], function () {

  Route::get('/dashboard', DashboardController::class)->name('dashboard');

  Route::controller(ShopController::class)->prefix('/shop')->group(function () {
    Route::get('/', 'index')->name('shop.index');
    Route::get('/{id}', 'show')->name('shop.show');
    Route::get('/calendar', 'calendar')->name('shop.calendar');
  });

  Route::controller(LeadController::class)->prefix('/lead')->group(function () {
    Route::get('/create', 'create')->name('lead.create');
    Route::get('/{lead_view}', 'index')->name('lead.index');
    Route::get('/{id}', 'show')->name('lead.show');
    Route::post('/', 'store')->name('lead.store');
    Route::get('/edit/{id}', 'edit')->name('lead.edit');
    Route::patch('/{id}', 'update')->name('lead.update');
    Route::post('/{id}', 'follow')->name('lead.follow');
    Route::delete('/{id}', 'destroy')->name('lead.destroy');
  });

  Route::controller(ClientController::class)->prefix('/client')->group(function () {
    Route::get('/create', 'create')->name('client.create');
    Route::get('/{client_view}', 'index')->name('client.index');
    Route::get('/{id}', 'show')->name('client.show');
    Route::post('/', 'store')->name('client.store');
    Route::get('/edit/{id}', 'edit')->name('client.edit');
    Route::patch('/{id}', 'update')->name('client.update');
    Route::delete('/{id}', 'destroy')->name('client.delete');
  });

  Route::controller(IntakeController::class)->prefix('/intake')->group(function () {
    Route::get('/edit/{id}', 'edit')->name('intake.edit');
    Route::post('/', 'store')->name('intake.store');
  });

  Route::controller(VehicleController::class)->prefix('/vehicle')->group(function () {
    Route::get('/show/{id}', 'show')->name('vehicle.show');
    Route::post('/{id}', 'store')->name('vehicle.store');
  });

  Route::controller(NoteController::class)->prefix('/note')->group(function () {
    Route::get('/show/{id}', 'show')->name('note.show');
    Route::post('/', 'store')->name('note.store');
    Route::post('/storeLead', 'storeLead')->name('note.storeLead');
    Route::post('/storeClient', 'storeClient')->name('note.storeClient');
  });

  Route::controller(DeviceController::class)->prefix('/device')->group(function () {
    Route::get('/show/{id}', 'show')->name('device.show');
    Route::post('/{id}', 'store')->name('device.store');
  });

  Route::controller(AppointmentController::class)->prefix('/appointment')->group(function () {
    Route::get('/show/{id}', 'show')->name('appointment.show');
    Route::post('/{id}', 'store')->name('appointment.store');
  });

  Route::controller(PaymentController::class)->prefix('/payment')->group(function () {
    Route::get('/show/{id}', 'show')->name('payment.show');
    Route::post('/{id}', 'store')->name('payment.store');
  });


});


require __DIR__ . '/auth.php';

And here is the Shop Controller

class ShopController extends Controller
{
    public function index()
    {
        $clients = Client::with(['vehicle', 'appointment'])
        ->where('default_location', 'home')->get(); 
        
        return view('shop.index',)->with(['clients' => $clients]);
    }

    public function calendar()
    {
        return "Testing...";

        // $clients = Client::with(['vehicle', 'appointment'])
        // ->where('default_location', 'home')->get();

        // return view('shop.calendar',)->with(['clients' => $clients]);
    }

Any reason this method would return a 404 ??? I have verified the route is working with route:list and cleared all cache. I've also tried renaming the method over a dozen times.

Thanks in advance.

CodePudding user response:

Probably because of the Route::get('/{id}', 'show')->name('shop.show'); route. The request is getting handled/intercepted by this route. The calender path parameter is passed to the this route and causing the issue.

Switch the routes and it should fix the issue. Place the shop.calender named route before the shop.show.

change:

Route::controller(ShopController::class)->prefix('/shop')->group(function () {
    ...
    Route::get('/{id}', 'show')->name('shop.show');
    Route::get('/calendar', 'calendar')->name('shop.calendar');
});

to:

Route::controller(ShopController::class)->prefix('/shop')->group(function () {
    ...
    Route::get('/calendar', 'calendar')->name('shop.calendar');
    Route::get('/{id}', 'show')->name('shop.show');
});

P.s: Also, if you could share your show method code, we can suggest you an answer without the need of switching the routes, via the type hinting.

  • Related