Can’t find the php controller with Laravel

Asked

Viewed 620 times

0

Hello guys I’m starting with the Standard, I’ve come across a problem. I created the controller, the routes, but even so, it’s giving me the following error:

Class App Http Controllers Contact2controller does not exist.

What should I do, guys? I already searched the net, but without success. Thanks.

For better understanding, the following is the code.

Controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class Contato2Controller.php extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('welcome');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function enviar(Request $request)
    {
        $contato = new Contato();
        $contato->nome = $request->get('nome');
        $contato->email = $request->get('email');
        $contato->mensagem = $request->get('mensagem');
        $contato->save();

        echo "Dados armazenados com sucesso! Código: " .$contato->id;
    }

    public function lista()
    {
        return view('lista', array('contatos'=>Contatos::all()));
    }
}

Routes:

Route::get('/','Contato2Controller@index');
Route::post('/enviar','Contato2Controller@enviar');
Route::get('/lista','Contato2Controller@lista');

OBS: I’m using version 5.1 of the Standard.

1 answer

1


Assuming the routes are correct and the naming of names is in accordance with the standards of the Laravel, the problem should be here:

class Contato2Controller.php extends Controller
{
...

left a . php, so there really is no Contact2controller controller (there is only Contact2controller.php). So solving is kind of like this:

class Contato2Controller extends Controller
    {
    ...

The Laravel has a command line tool to make the automatic generation of both the controller and the route, called Artisan.

  • Nor is it a Laravel pattern problem, PHP does not actually recognize points in class names, functions and namespaces. This actually emits something like Parse error: syntax error, unexpected '.', expecting '{'.

  • @Guilhermenascimento was about to comment, the strange thing is that in the question itself this is not part of the problem.

  • @Edilson probably the user is or Laravel issued that custom error without details (maybe AP accidentally turned off debug mode)

  • But to display the syntax errors cited above the file in question should be executed. Probably the file name does not match the class name.

  • Okay guys! I took all the tips given, created another controller (contact3Controller), copied the code from the previous controller and pasted it into this one. That error disappeared, and another one came up: Fatalerrorexception in contact3Controller.php line 24: Class 'Http Controllers App Contact' not found This error occurs when I try to record something in the bd.

  • You did not create a class called Contact, in the model folder of the Laravel and did not import this class in the Contact3controller controller. Take a look in this and in this start guide.

  • Thank you very much @Juven. I actually had already created the class, I just needed to care about it in the controller. That ended and it worked, it’s working. Thanks!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.