问题描述:

I'm trying to learn Laravel, and I created controller (using artisan's php artisan controller:make AlbumController). Then, I added some functions there, mainly function parse:

/**

* Parse the album.

*

* @param int $id

* @return Response

*/

public function parse($id)

{

return 'http://api.deezer.com/'.$id;

}

In routes.php I added

Route::resource('album', 'AlbumController');

But when I try to access the parse page (http://localhost/album/parse/123) Laravel returns throw new NotFoundHttpException();.

What did I do wrong?

网友答案:

parse is not an included route in Laravel's resourse controller. Run php artisan routes to see your current route structure.

If you want to use the parse method in your controller you should define the route manually. Add something like

Route::get('album/parse/{id}', ['uses' => '[email protected]']);

to your routes file.

As an aside, the resource controller can be a handy way to get your CRUD routes up and running but it is good practice to define most of your routes explicitly as your routes.php file is useful documentation for your application and it makes the workings of it much easier to follow.

网友答案:

You need to change the route to

Route::get('album/parse/{id}', '[email protected]');

More of routing with parameters can you find HERE inside the Laravel Docs

And the docs with some information about Routing with Controllers

An little part of my route to give you an idea of how it could look:

Route::get('/partijen/nieuw', '[email protected]');
Route::post('/partijen/nieuw', '[email protected]_new');
Route::get('/partijen/edit/{id}', '[email protected]');
Route::post('/partijen/edit/{id}', '[email protected]_edit');
相关阅读:
Top