Laravel 9路线不使用自定义方法

z2acfund  于 2022-11-18  发布在  其他
关注(0)|答案(1)|浏览(144)

我有一个很大的路线文件。除了shop.calendar(返回404)之外,我所有的路线都工作正常。我真的很困惑!
这是我的路线文件

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

这位是车间主任

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]);
    }

这个方法会返回404的原因是什么???我已经验证了这个路由是使用route:list的,并且清除了所有缓存。我也尝试过十几次重命名这个方法。
先谢谢你。

k3fezbri

k3fezbri1#

可能是因为Route::get('/{id}', 'show')->name('shop.show');路由。此路由正在行程/拦截要求。calender路径参数会传递给此路由,因此造成问题。
切换路由,应该可以解决问题。将shop.calender命名路由放在shop.show之前。

更改:

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

收件人:

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

**附言:**此外,如果您可以分享您的show方法代码,我们可以通过类型提示为您提供答案,而无需切换路径。

相关问题