【问题标题】:Laravel, routingLaravel,路由
【发布时间】:2017-05-22 21:38:39
【问题描述】:

这是我的路线:

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>

但是,当我点击这个时,我给出了警告:

ErrorException in Category.php line 16:
Trying to get property of non-object

而且我认为,这个类别不需要,我想,这个 href 正在进入他的路线:

Route::get('/{categoryName}', 'StoreController@showCategory'); 

是否有可能修复它?也许将动作添加到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');

 }

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

【问题讨论】:

  • Category.php的内容是什么?那是你的StoreController吗?
  • /cart/add 正在等待一个 post 请求,你并通过 a href 传递一个 get 请求
  • 我认为您正在尝试在不存在的地方获得价值,您可以分享 category.php 代码吗?
  • 我在帖子中添加所有内容(已编辑帖子)

标签: laravel routing


【解决方案1】:

您的类别路线是一条包罗万象的路线。如果它们未在其后定义或定义,这将捕获具有相似路径的所有路由。在这种情况下,/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/addGET 路由。您可能需要这些路线。

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

【讨论】:

  • 我是这么认为的!但我不知道如何解决它。现在真的有效。谢谢你!
  • This will overwrite all other routes with the similar path since these are defined at the bottom. 实际上正好相反——因为它位于底部,这将捕获与上述任何其他内容不匹配的任何请求。路由从文件顶部按顺序匹配。他们在这种情况下匹配的原因是因为没有为该 URI 定义 GET 路由。你不需要移动它们,或者更新它们,至少不需要解决这个问题——如果你这样做了,你只会得到一个 404,因为现在根本没有匹配的路线。
  • @Don'tPanic 但我尝试更改帖子以获得相同的问题。更改 url 解决这个问题。
【解决方案2】:

您的“添加到购物车”链接是一个普通的&lt;a href=''&gt; 链接,因此单击它会执行GET 请求/cart/add。但该 URI 唯一定义的路由是 POST

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

所以它不匹配,下一个可能的匹配是你的通配符/{categoryName} 路由,它将捕获任何东西。

您需要:

  • 更新添加到购物车链接,改为将POST 改为/cart/add,例如,可以添加带有Add to cart 按钮的表单,或者让一些JS 捕捉点击并执行AJAX @987654330 @;

  • /cart/add添加一个GET路由,并让该方法通过GET而不是POST处理添加产品;

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 2020-04-05
    • 2021-08-17
    相关资源
    最近更新 更多