Help with laravel 5.1

0

I'm starting to study laravel 5.1, I'm excited about this but when I create a controller (contactController)

<?php 

namespace app\Http\Controllers;
use app\Http\Controllers\Controller;

class ContactoController extends Controller
{
 
    public function inicio()
    {
        return "esto es un controlador";
    }
}

in my route I do the following

Route::get('contacto','ContactoController@inicio');

I get the following error:

Class 'app\Http\Controllers\Controller' not found

    
asked by MasSoft 18.07.2016 в 04:03
source

4 answers

1

Any particular reason why you are using Laravel 5.1? (the latest stable version with excellent improvements is laravel 5.2).

just to be sure, when you installed Laravel the main page came out (where the logo and some phrases appear)? (to know if the installation worked).

If the installation is correct try the following:

1) rename your controller ContactController.php to Contactcontroller-copia.php

2) Run from the Artisan console to be generated by the Controller.

Running the command:

php artisan make:controller ContactoController

3) in the routes.php file includes the route

Route::get('contacto','ContactoController@inicio');

4) content of the controller and the start method ..

...
class ContactoController extends Controller
{

    public function inicio()
    {
        return "esto es un controlador";
    }
}...

I hope you find it useful. Greetings ...

    
answered by 18.07.2016 в 05:50
1

From what I see in the image you share, the namespace starts with cinema and not app .

I assume that the error you get when going to the path /contacto ?

In summary, the error should be in the namespace, so check the namespace you have in composer.json:

"autoload": {
    ...
    "psr-4": {
        "cinema\": "app/"
    },

Or you can change the namespace with the command:

php artisan app:name cinema

So if the base namespace is cinema , the contact controller (and all other controllers) should look like this:

<?php 

namespace cinema\Http\Controllers;

use Controller; // el controlador base está en el mismo namespace

class ContactoController extends Controller
{
   // ...
}
    
answered by 18.07.2016 в 06:50
0

Looking at your code, I realized that your namespace is wrong

namespace app\Http\Controllers;

It should be like this

namespace App\Http\Controllers;

A laravel is case sensitive

    
answered by 04.01.2017 в 22:36
0

Theory

The problem lies in the namespaces, when Laravel gives an error of:

Class 'app\Http\Controllers\Controller' not found

It's often a mistake that en route is incorrectly redirecting the call to where it has to go.

The namespaces in Laravel are very important they are a standard from the PSR4 , where they comment, together with the PSR2 .

  

Laravel follows the PSR-2 coding standard and the PSR-4 autoloading   standard.

Source

Solution

namespace App\Http\Controllers;
class ContactoController extends Controller
{

    public function inicio()
    {
        return "esto es un controlador";
    }
}

Your error is in:

namespace cinema\Http\Controllers;

Logically if Laravel starts in App \ Http \ Controllers, the cinema does not pick it up.

I advise you to use the artisan from Laravel, which is a way that builds all the models and controllers in a standard way, as you want them to be used, with time you will notice how it is done and you will not need the command, the command would be:

php artisan make:[model|controller] --resource Evento

php artisan make:controller --resource ContactoController

Link | Official Source

  • - resource what it does is create all the standard headers that Laravel uses.

This you have to run in the root directory where you have installed laravel, which is one level below the App folder that is where the Namespaces starts to write the address where they are.

Curiosity

I'll put some notes for you to know the reason of the namespaces:

  • The namespaces are necessary so that there is no clash between the classes, for example if you have two classes called as in your case ContactController, then you have unnecessary crebaderos head.
  • Another reason is to define in a perfect way where each class is located, for when you make a use, in the code you do it to the class you want.
  • PD

    I hope I have helped, through this response and do not hesitate to ask, we do not all know everything, but we all know it.

        
    answered by 29.08.2017 в 22:11