How to add new methods

2

Good morning

At the moment I am using a controller type route::resouce , as you well know this adds certain methods to facilitate the work, as index, create, edit, destroy . The problem that I present is that you add a custom method.

  public function lista()

   {
        $tipos_usuarios = \DB::table('tipos_usuarios')
                             ->select('id', 'usuarios_rol')
                             ->get();
        return view('auth.register')->with('tipos_usuarios', $tipos_usuarios);
    }

But for some reason, the driver does not read my new method, maybe you have to configure it in the root of laravel, so that it is recognized?.

    
asked by zereft 06.10.2018 в 02:58
source

1 answer

2

The controller of type resource is created with the following command

php artisan make:controller BlogController --resource

Later on at the level of routes we use it as follows

Route::resource('/saludo', 'BlogController');

The previous Controller BlogController , has the methods:

  • destroy
  • update
  • edit
  • show
  • store
  • create
  • index
  • However now I am going to declare a new method called sayHi() as follows ( this method does not belong to the lsite of regular HTTP verbs )

     public function sayHi()
     {
         return "Hola a todos y todas";
     }
    

    What do I call it now?

    It simply identifies the HTTP verb that it requires and generates its new route, just before the call to Route::resource

    It should look like this

    Route::get('/saludos', 'BlogController@sayHi');
    Route::resource('/saludo', 'BlogController');
    

    Remarks

      
  • The call to the custom method, that is, the one you create must go to the beginning
  •   
  • the busy controller is the same but at the end with the @ marker we point to the name of the method we want to invoke
  •   
        
    answered by 06.10.2018 / 03:23
    source