Laravel 拉维尔,路由

Laravel 拉维尔,路由,laravel,routing,Laravel,Routing,这是我的路线: Route::get('/', 'StoreController@mainSite'); Route::get('/product/{id_product}', 'StoreController@showProduct'); Route::get('/kontact', 'StoreController@showContaktForm'); Route::get('/login', 'AuthController@formView') Route::post('/login

这是我的路线:

Route::get('/', 'StoreController@mainSite');

Route::get('/product/{id_product}', 'StoreController@showProduct');
Route::get('/kontact', 'StoreController@showContaktForm');


Route::get('/login', 'AuthController@formView')
Route::post('/login', 'AuthController@login');
Route::get('/logout', 'AuthController@logout');

Route::get('/register', 'RegisterController@formView');
Route::post('/register', 'RegisterController@register');

Route::get('/panel', 'PanelController@mainSite');
Route::get('/panel/data', 'PanelController@formView');
Route::post('/panel/data', 'PanelController@updateData');


Route::get('/panel/orders', 'OrderController@showOrders');
Route::post('/panel/orders/add', 'OrderController@addOrder');

Route::post('/cart/add', 'CartController@addItem');
Route::post('/cart/remove', 'CartController@removeItem');
Route::get('/cart', 'CartController@showItems');


Route::get('/{categoryName}', 'StoreController@showCategory');
Route::get('/{categoryName}/{subcategoryName}','StoreController@showSubcategory');
现在,我的视图中有一个代码链接,可以将项目添加到购物车中:

<h4><a class="shopBtn" href="/cart/add" title="add to cart"> Add to cart </a> </h4>
我认为,这个类别在这方面是不必要的,我想,这个a href将进入他的路线之一:

Route::get('/{categoryName}', 'StoreController@showCategory'); 
有可能修复它吗?也许可以向a href添加操作? 我很确定在我的错误中调用的这个模型和我现在想做的事情没有关系

我的类别模型:

class Category extends Model
{
protected $table = 'zoo';
public $timestamps = false;

public function getCategoryId($name)
{

    $category = Category::select('id')->where('name', $name)>first();

    return $category->id;


}
}

但是我的CartController应该返回这个视图

  public function addItem()
  {

   return View('cart');

 }
我认为它无论如何都不会到达这里

您的“添加到购物车”链接是一个普通的
链接,因此单击它会执行
获取
请求
/cart/Add
。但该URI的唯一定义路由是用于
POST

Route::post('/cart/add', 'CartController@addItem');
因此它不匹配,下一个可能的匹配是通配符
/{categoryName}
路由,它将捕获任何内容

您需要:

  • 更新“添加到购物车”链接,改为执行
    POST
    /cart/add
    ,例如,可以添加一个带有
    添加到购物车
    按钮的表单,或者让一些JS点击并执行AJAX
    POST

  • 或者为
    /cart/Add
    添加
    GET
    路径,并让该方法通过
    GET
    而不是
    POST
    处理添加产品的操作

您的“添加到购物车”链接是一个普通的
链接,因此单击它会执行
获取
请求
/cart/Add
。但该URI的唯一定义路由是用于
POST

Route::post('/cart/add', 'CartController@addItem');
因此它不匹配,下一个可能的匹配是通配符
/{categoryName}
路由,它将捕获任何内容

您需要:

  • 更新“添加到购物车”链接,改为执行
    POST
    /cart/add
    ,例如,可以添加一个带有
    添加到购物车
    按钮的表单,或者让一些JS点击并执行AJAX
    POST

  • 或者为
    /cart/Add
    添加
    GET
    路径,并让该方法通过
    GET
    而不是
    POST
    处理添加产品的操作


您的分类路线是一条包罗万象的路线。如果未定义或未在其之后定义路径,则这将捕获具有类似路径的所有路径。在这种情况下,
/cart/add
路径满足
/{categoryName}/{subcategoryName}
的条件,因此
StoreController@showSubcategory
被调用。您需要删除或移动它们以防止路由冲突

Route::get('/{categoryName}', 'StoreController@showCategory');
Route::get('/{categoryName}/{subcategoryName}','StoreController@showSubcategory');
可能会改成

Route::get('/category/{categoryName}', 'StoreController@showCategory');
Route::get('/category/{categoryName}/{subcategoryName}','StoreController@showSubcategory');
您还缺少
/cart/add
GET
路线。你可能需要这些路线

Route::get('/cart/add', 'CartController@addItem');
Route::post('/cart/add', 'CartController@storeItem');

您的分类路线是一条包罗万象的路线。如果未定义或未在其之后定义路径,则这将捕获具有类似路径的所有路径。在这种情况下,
/cart/add
路径满足
/{categoryName}/{subcategoryName}
的条件,因此
StoreController@showSubcategory
被调用。您需要删除或移动它们以防止路由冲突

Route::get('/{categoryName}', 'StoreController@showCategory');
Route::get('/{categoryName}/{subcategoryName}','StoreController@showSubcategory');
可能会改成

Route::get('/category/{categoryName}', 'StoreController@showCategory');
Route::get('/category/{categoryName}/{subcategoryName}','StoreController@showSubcategory');
您还缺少
/cart/add
GET
路线。你可能需要这些路线

Route::get('/cart/add', 'CartController@addItem');
Route::post('/cart/add', 'CartController@storeItem');

Category.php
的内容是什么?是您的
StoreController
/cart/add
正在等待一个post请求,而您正在通过
a href
传递一个get请求。我想您是在尝试获取不存在的值,您可以共享category.php代码吗?我在post中添加了所有内容(编辑的post)
category.php
?是不是你的
StoreController
/cart/add
正在等待一个post请求,而你正在通过
a href
传递一个get请求。我想你是在尝试获取不存在的值,你能共享category.php代码吗?我在post(编辑的post)中添加了所有内容,我想是这样的!但我不知道如何修复它。现在真的有用了。谢谢你<代码>这将覆盖具有类似路径的所有其他路由,因为这些路由是在底部定义的。
实际上情况正好相反-因为它位于底部,这将只捕获与上面任何其他路径都不匹配的任何请求。从文件顶部开始,按顺序匹配路由。在本例中,它们匹配的原因是bcs没有为该URI定义GET路由。你不需要移动或更新它们,至少不需要解决这个问题-如果你这样做,你只需要得到一个404,因为现在根本没有匹配的路由。@don'tPanic,但我试着改变帖子来获取,我仍然有同样的问题。改变url解决这个问题。我想是的!但我不知道如何修复它。现在真的有用了。谢谢你<代码>这将覆盖具有类似路径的所有其他路由,因为这些路由是在底部定义的。
实际上情况正好相反-因为它位于底部,这将只捕获与上面任何其他路径都不匹配的任何请求。从文件顶部开始,按顺序匹配路由。在本例中,它们匹配的原因是bcs没有为该URI定义GET路由。你不需要移动或更新它们,至少不需要解决这个问题-如果你这样做,你只需要得到一个404,因为现在根本没有匹配的路由。@don'tPanic,但我试着改变帖子来获取,我仍然有同样的问题。更改url以解决此问题。