Problem in laravel "Invalid route action: [App \ Http \ Controllers \ HomeController]."

2

Hello create a driver with

PHP artisan make: controller HomeController --resource HomeController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

, I already have my eyesight

then I enter it in web.php

Route :: get ('home', 'HomeController') - > name ('home');

and I get the error "Invalid route action: [App \ Http \ Controllers \ HomeController] .."

    
asked by Naoto Amari 06.03.2018 в 18:01
source

4 answers

2

When creating a route either GET or POST in Laravel you should indicate which function you should go to and not just pass the complete controller, which is what you do.

When doing:

  

Route :: get ('home', 'HomeController') - > name ('home');

You do not specify to which function within HomeController you point, to solve this you change it for the following:

Route::get('home', 'HomeController@index')->name('home.index');

In case you have a route type POST you do it in a similar way. Ex:

Route::post('home', 'HomeController@store')->name('home.store');

In case you need to use the whole CRUD set. you use the resource that creates all the routes in a single command.

Route::resource('home', 'HomeController');

The list of routes created would be:

-------------------------------------------------------------------
* Method |   URI            |    Name      |    Action            |  
-------------------------------------------------------------------
| GET    | home             | home.index   |HomeController@index  |
| POST   | home             | home.store   |HomeController@store  |
| GET    | home/create      | home.create  |HomeController@create | 
| PUT    | home/{home}      | home.update  |HomeController@update |
| DELETE | home/{home}      | home.destroy |HomeController@destroy|
| GET    | home/{home}      | home.show    |HomeController@show   |
| GET    | home/{home}/edit | home.edit    |HomeController@edit   |
-------------------------------------------------------------------
    
answered by 06.03.2018 / 19:25
source
2

You need to specify the action in the route that indicates the method in the controller to which the route will access:

Route::get('home', 'HomeController@index')->name('home');
    
answered by 06.03.2018 в 18:22
1

that seems to be a resource that you created in laravel

the serious way

Route::resource('home', 'HomeController');

    
answered by 06.03.2018 в 18:27
0

You only need the controller method you want to refer to, it would be:

Route:get('home', 'HomeController@accion')->name('home'); 

the way to use that route without specifying a controller action is to implement the __invoke method in the controller.

    
answered by 06.03.2018 в 19:06