Domanda

I have seen the below functionality and I don't understand how functions in controller is choosed?

class ProfilesController extends \BaseController {

  public function index() {}

  public function create() {}

  public function store(){}

  public function show($id){}

  public function edit($id){}

  public function update($id){}

  public function destroy($id){}

   }

For local.com/profiles it would call index() function and list all the profiles. For viewing any record local.com/profiles/99 it is using show() method. For editing any record local.com/profiles/99/edit it is using edit().

Are these methods are created automatically? Please suggest me any link or document which helps in understanding Laravel better.

È stato utile?

Soluzione

The links provided to you are good to understand how to implement restful urls in Laravel but you don't know what is restful.

The method naming chosen by Laravel it's a convention used to represent what each method does. It's called CRUD.

Now which method is been called depends on the HTTP Request Method.

GET     /resource                   index   resource.index
GET     /resource/create            create  resource.create
POST    /resource                   store   resource.store
GET     /resource/{resource}        show    resource.show
GET     /resource/{resource}/edit   edit    resource.edit
PUT/PATCH   /resource/{resource}    update  resource.update
DELETE     /resource/{resource}     destroy resource.destroy

To avoid redundant code when we have a CRUD we use resource controller.

You have to add the below route to your routes.php and the controller you have already provide.

Route::resource('profile', 'ProfilesController');

It's the same as writing

Route::get('profile', 'ProfilesController@index'));
Route::get('profile/create', 'ProfilesController@create'));
Route::post('profile', 'ProfilesController@store'));
Route::get('profile/{id}', 'ProfilesController@show'));
Route::get('profile/{id}/edit', 'ProfilesController@edit'));
Route::put('profile/{id}', 'ProfilesController@update'));
Route::patch('profile/{id}', 'ProfilesController@update'));
Route::delete('profile/{id}', 'ProfilesController@destroy'));

If you want to generate those lines. You can use Jeffreys Way generators.

See Teach a Dog to REST to understand what I'm talking about.

Altri suggerimenti

You can look at github Laravel page at Router.php -> https://github.com/laravel/framework/blob/master/src/Illuminate/Routing/Router.php

Check the resourceDefaults and addResource* functions :)

-- And of course go to Laravel documentation > http://laravel.com/docs/controllers there is the info which you need for your work..

I assume you are using Resource Controllers, http://laravel.com/docs/controllers#resource-controllers.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top