目前,我正在編寫基於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