2 votes

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

Hello, I created a controller 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 ].."

4voto

Shassain Points 2710

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

By doing:

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

You do not specify to which function inside 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 paths in one 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   |
-------------------------------------------------------------------

2voto

Shaz Points 22941

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

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

1voto

that looks like a resource you created in laravel

the serious way

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

0voto

Francisco Quijada Points 809

You are only missing 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.

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X