【问题标题】:Laravel Resource Routing not showing anything when directly call method直接调用方法时,Laravel 资源路由不显示任何内容
【发布时间】:2021-01-06 21:48:08
【问题描述】:

我卡在资源路由中 当我输入 url netbilling.test/customer 时,它会转到客户索引文件,但是当我输入 url netbilling.test/customer/index 时,没有返回任何内容。如果我必须路由与资源中不同的方法,也请指导我。

这是我的 web.php,

Route::get('/dashboard', function () {
    return view('dashboard/index');
});
Route::resource('/customer','CustomerController');

这是我的客户控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Customer;
use App\Package;
use Redirect,Response;

class CustomerController extends Controller
{
    public function index()
    {
        $packages = Package::get();
        $customers = Customer::orderBy('id', 'DESC')->get();
        return view('customer/index', compact('customers','packages'));
    }
    public function create()
    {
        //
    }
    public function store(Request $request)
    {
       //
    }
    public function show($id)
    {
        //
    }
    public function edit($id)
    {
        //
    }
    public function update(Request $request, $id)
    {
        //
    }
    public function destroy($id)
    {

    }
 }

【问题讨论】:

  • 您需要在您的路由文件中将路由添加到 /customers/index。您只有到 /customer 的路线。这就是为什么只有 /customer 工作
  • customer/index 将匹配 customer/{customer} 这是您的 show 路由,它什么都不返回......您缺少该控制器的 destroy 方法。顺便说一句
  • Kofi Mokome 我正在使用资源路由,所以我只使用 /customer..

标签: laravel routes


【解决方案1】:

没有自定义路由规范,这就是索引路由映射到资源控制器的方式,取自Actions Handled By Resource Controller

Verb URI Action Route Name
GET /photos index photos.index

因此,如果您希望 URI /customer/index 起作用,则需要在控制器中明确指定:

use App\Http\Controllers\CustomerController;

Route::resource('customer', CustomerController::class);
Route::get('customer/index', [CustomerController::class, 'index'])->name(customer.index);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    • 2020-09-04
    • 2018-07-11
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    相关资源
    最近更新 更多