目前,我正在编写基于Laravel的电子商务应用程序。我的项目的主导航结构如下:Laravel - 类别导航路由错误
- 首页(/)
- 类别1(例如/汽车)
- 类别2(例如/汽车/大众)
- 3类(如/汽车/大众/高尔夫)
该模型的名称是'类别'和控制器是'类别控制器'。型号表产生与迁移:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('parent_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
为此我创建了以下航线web.php:
Route::get('/{category1}', '[email protected]');
Route::get('/{category1}/{category2}', '[email protected]');
Route::get('/{category1}/{category2}/{category3}', '[email protected]');
这是我CategoryController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category as CategoryModel;
class CategoryController extends Controller
{
public function category1(CategoryModel $category1)
{
// Do some stuff
}
public function category2(CategoryModel $category1, CategoryModel $category2)
{
// Do some stuff
}
public function category3(CategoryModel $category1, CategoryModel $category2, CategoryModel $category3)
{
// Do some stuff
}
}
最后,像localhost/cars/volksw这样的页面agen/golf可以打开。问题是如果我在浏览器中请求本地主机/高尔夫,服务器会将页面发送给我。该页面不是我想要的。这个页面只能通过localhost/cars/volkswagen/golf。
你有一些建议如何解决这个问题?
编辑1:我仍然想要页/ category1 /和/ category1/category2 /交付。本页面显示可用产品的概览。
您是否验证其功能,它实际上正在经历?尝试'Log :: info(__ FUNCTION __);'作为每个函数的第一行,然后在浏览到该页面后检查日志。 – aynber
@aynber它正在通过所有的功能: [2017-09-01 05:50:24] local.INFO:category1 [2017-09-01 05:50:30] local.INFO:category2 [ 2017-09-01 05:50:35] local.INFO:category3 – glembo