【问题标题】:Laravel doesn't return edit viewLaravel 不返回编辑视图
【发布时间】:2018-01-21 17:07:24
【问题描述】:

我使用 laravel 5.5,我创建了一个视图,用于在 views/back/categories/edit 中编辑类别 这是我在 CategoriesController 中的编辑功能

 public function edit($id)
    {
        $category = Categories::getall();
        $categories = Categories::find($id);
        return view('back.categories.edit', ['categories' => $categories, 'category' => $category]);
    }

这是我的路线

Route::group(['middleware'=>'admin'],function(){

    Route::get('/dashboard','BackendController@index')->name('backend');
    Route::group(['prefix' => 'categories'], function () {
        Route::any('/show/{id}', ['as' => 'backend.categories.show', 'uses' => 'backend\CategoriesController@show']);
        Route::get('/index', ['as' => 'back.categories.index', 'uses' => 'backend\CategoriesController@index']);
        Route::any('/store', ['as' => 'back.categories.store', 'uses' => 'backend\CategoriesController@store']);
        Route::any('/create', ['as' => 'back.categories.create', 'uses' => 'backend\CategoriesController@create']);
        Route::any('/edit/{id}', ['as' => 'back.categories.edit', 'uses' => 'backend\CategoriesController@edit']);
        Route::any('/update', ['as' => 'back.categories.update', 'uses' => 'backend\CategoriesController@update']);
        Route::any('/destroy/{id}', ['as' => 'back.categories.destroy', 'uses' => 'backend\CategoriesController@destroy']);
    });

});

我的编辑按钮

    <a href="{{ url('back/categories/edit/'.$category->cat_id) }}" class="btn btn-success btn-sm">
<span class="fa fa-edit"></span> edit</a>

当我单击编辑按钮时,它会返回“抱歉,找不到您要查找的页面”的页面。文本和 URL :"back/categories/edit/4"

【问题讨论】:

  • 这些路由是否使用back 前缀?
  • 我不这么认为,我的索引和创建路由工作,但我不知道为什么我的编辑路由不能返回视图。我试图看到我的视图有问题,但它如果我将该视图放在其他要查看的路线中仍然有效,但不是编辑路线
  • 请显示指向index 路由的链接示例。
  • url('/back/categories/edit/'.$category-&gt;cat_id)
  • @Sohel0415 这将创建完全相同的完整 URL。

标签: php laravel


【解决方案1】:

由于您的路线已命名,您可以使用 route() 帮助器来构建工作 URL:

{{ route('back.categories.edit', ['id' => $category->cat_id]) }}

【讨论】:

    【解决方案2】:

    从您的路线定义看来,您的网址是 /categories/edit/{id}

    对于模板中的链接,我建议使用 route 辅助函数,它采用路由名称或 action 方法,采用 controller@method

    也看看资源控制器

    Route::resource('categories', 'CategoryController'); 可以替换你的整个路由定义

    https://laravel.com/docs/5.5/controllers#resource-controllers

    【讨论】:

      【解决方案3】:

      您的categories 组中缺少前缀的back 部分。

      Route::group(['prefix' => 'back/categories'], function () {
      

      这将匹配您的路线。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-05-21
        • 2014-02-10
        • 2016-02-02
        • 2016-05-16
        • 2017-09-15
        • 1970-01-01
        • 2015-11-12
        • 1970-01-01
        相关资源
        最近更新 更多