【发布时间】:2021-10-01 09:35:53
【问题描述】:
我正在使用 laravel 8,当我这样写路由时:
web.php
Route::match(['get', 'post'], '/', [PageController::class, 'index']);
Route::match(['get', 'post'], '/tshirt', [PageController::class, 'productCategory']);
Route::match(['get', 'post'], '/electronic', [PageController::class, 'productCategory']);
Route::match(['get', 'post'], '/tshirt/{slug}', [PageController::class, 'detail']);
Route::match(['get', 'post'], '/electronic/{slug}', [PageController::class, 'detail']);
和这样的控制器:
PageController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PageController extends Controller
{
public function index()
{
$data = [
'title' => 'Dashboard',
];
return view('pages.index', $data);
}
public function productCategory($category)
{
# code...
}
public function detail($category, $detail)
{
# code...
}
}
我想要的是,如果当用户位于 url '/tshirt' 或 '/electronic' 时,thshirt 或 electronic 将包含在 PageController::productCategory 的 $category 参数中
类似地,当用户输入“/tshirt/nameoftshirt”时,PageController::detail 控制器将用“tshirt”填充$category 参数,用“nameoftshirt”填充$slug
它显示了这个错误:
ArgumentCountError
Too few arguments to function App\Http\Controllers\PageController::productCategory(), 0 passed in D:\Programing\Web\laraApp\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected
如何获取路由以将主要参数传递给控制器?
【问题讨论】:
标签: php laravel laravel-8 laravel-routing laravel-controller