【问题标题】:Laravel Route custom route instead of resourceLaravel Route 自定义路由而不是资源
【发布时间】:2016-08-02 10:18:44
【问题描述】:

我正在使用 laravel 5.2,我正在尝试从我的视图中获取到我的 AdminOfficesController 的自定义链接。但是由于某种原因,它只是给了我一个没有错误或任何错误的空白页。尝试使用文档、stackoverflow 和调试对其进行调试,但我仍然不知道为什么它不起作用。任何帮助表示赞赏。

在我看来,我有以下链接:

                    <a href="{{ url('admin/offices/' . $customer->id)  }}">
                        <div class="float-left">
                            <button class="btn btn-success btn-xs">Create office</button>
                        </div>
                    </a>

在我的路线中,我有以下代码:

Route::get('/', function () {
    return view('/auth/login');
});

Route::auth();

Route::get('/home', 'HomeController@index');


Route::resource('admin/users', 'AdminUsersController');
Route::resource('admin/customers', 'AdminCustomersController');
Route::resource('admin/offices', 'AdminOfficesController');
Route::resource('admin/labourentries', 'AdminLabourentriesController');
Route::resource('admin/labourtypes', 'AdminLabourtypesController');
Route::get('/admin/dashboard', 'AdminCustomersController@dashboard');
Route::get('admin/offices/{customer_id}/create',  'AdminOfficesController@create');

我的控制器包含以下代码:

public function create()
{
    echo "hi";
    return view('admin.offices.create');
}

我可能在我的视图或路由文件中遗漏了一些非常简单的东西,但老实说我没有看到它。我的控制器的其他功能(如索引等)工作正常。

感谢您的帮助。 罗德尼

【问题讨论】:

    标签: php laravel routes


    【解决方案1】:

    你的按钮是:

                    <a href="{{ url('admin/offices/' . $customer->id)  }}">
                        <div class="float-left">
                            <button class="btn btn-success btn-xs">Create office</button>
                        </div>
                    </a>
    

    你的路线是:

    admin/offices/{customer_id}/create

    你忘了/create

    我建议您为您的路线使用名称(更多关于 laravel 文档),因为如果您更改 URL,每条路线都将拥有新的 URL,无需任何更改

    【讨论】:

      【解决方案2】:

      你可以命名你的路线:

      Route::get('/admin/offices/{customer_id}/create', [
          'as' => 'admin.offices.customer.create,
          'uses' => 'AdminOfficesController@create'
      ]);

      并像这样使用它

      &lt;a href="{{URL::route('admin.offices.client.create', array($customer-&gt;id))}}"&gt;

      【讨论】:

      • 不客气,实际上 Md. Sahadat alfo 发现了你犯的一个错误。但是使用命名路由是要走的路,所以这是可取的
      【解决方案3】:

      修改你的html代码

      <a href="{{ url('admin/offices/' . $customer->id.'/create')  }}">
                              <div class="float-left">
                                  <button class="btn btn-success btn-xs">Create office</button>
                              </div>
                          </a>
      

      【讨论】:

        【解决方案4】:

        路由资源只是为了让你的代码保持干爽,但如果你需要进行更改,你可以随时使用完整的代码并随意更改。

        Route::get('resource', ['as' => 'resource.index', 'uses' => 'ResourcesController@index']);
        Route::get('resource/create', ['as' => 'resource.create', 'uses' => 'ResourcesController@create']);
        Route::post('resource', ['as' => 'resource.store', 'uses' => 'ResourcesController@store']);
        Route::get('resource/{resource}', ['as' => 'resource.show', 'uses' => 'ResourcesController@show']);
        Route::get('resource/{resource}/edit', ['as' => 'resource.edit', 'uses' => 'ResourcesController@edit']);
        Route::put('resource/{resource}', ['as' => 'resource.update', 'uses' => 'ResourcesController@update']);
        Route::patch('resource/{resource}', ['uses' => 'ResourcesController@update']);
        Route::delete('resource/{resource}', ['as' => 'resource.destroy', 'uses' => 'ResourcesController@destroy']);
        

        【讨论】:

          猜你喜欢
          • 2015-05-20
          • 1970-01-01
          • 1970-01-01
          • 2017-05-22
          • 2011-01-17
          • 2011-07-05
          • 2015-11-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多