8
votes

I am new here but I already checked all solutions about my problem here and still didn't fix it.

I want to create simple app with tutorial in Laravel 5.2, and I can't make my controller to work.

I named my app "test" and here is a code:

PagesController.php:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class PagesController extends Controller
{
    public function getAbout(){
         return view('about');   
    }
}

routes.php:

Route::get('about', [
    'as' => 'about',
    'uses' => 'PagesController@getAbout'
]);

And Controller.php (default):

<?php

namespace test\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

Do you see any problem here? I am sure all files are in correct folders.

5
did you see that namespace in the controller.php? test\http\.... - PlayMa256
yes I see it, but it's just default... nothing changes if I change it to test\app\http\... - Ciapss
lol you are supposed to replace app with test. Sometimes you need to do a composer dump-autoload command after renaming your app or have new classes. - TurtleTread
and its not test\app, app is a generic name given when you create your first app. Just do what ive posted, and i should fix your errors. - PlayMa256
I changed it to test\http\... but still get the same error. - Ciapss

5 Answers

10
votes

Error can also occur if App/Http/Controllers/ folder does not have Controller.php file.

Make sure file exists.

3
votes

To make everything correct, run this.

php artisan app:name YourApplicationName 

it is going to change everything with app to your application name, so you wont need to write manually test everywhere

2
votes

Please correct your namespace in your pagecontroller

<?php


namespace test\Http\Controllers;

use test\Http\Controllers\Controller;

class PagesController extends Controller
{
    public function getAbout(){
         return view('about');   
    }
}

UPDATE :

After change in namespace in controller please dump-autoload your composer:

Composer dump-autoload

0
votes

My controller now looks like this:

<?php

namespace test\Http\Controllers;

use test\Http\Controllers\Controller;

class PagesController extends Controller
{
    public function getAbout(){
     return view('about');   
    }
}

And error is still there

Update:

Okay, I created a new project and it finnaly works. I think all ansewrs will be helpful for similiar problems :)

Thanks for help !

0
votes

In my case I had two namespaces one for the controllers and one for the models in the controller.php

namespace App\Http\Controllers;

namespace App\Http\Models;

second one was interfering with the first

I removed the second one and voila ... code worked