controller in Laravel - php

I trying to learn Laravel but I have a problem that I don't understand.
I get an error message when I want to run the controller.
I enter 3 lines of command on CMD
php artisan make:controller WelcomeController
php artisan make:controller AboutController
php artisan serve
Then, I establish the link between the controller and the route.
Route::get('about', 'AboutController#index');
And in the file AboutController.php I have
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class AboutController extends Controller
{
public function index()
{
return view('about');
}
}
Normally it should work ? I looked a tuto on youtube https://www.youtube.com/watch?v=QASe8bXMMFA&t=56s
I have as error message => "include(C:\wamp64\www\testprojet\vendor\composer/../../app/Http/Controllers/Controller.php): failed to open stream: No such file or directory"
Do you have an idea please ?

Your error indicates that the Controller.php that your AboutController extends.
Is there a Controller.php file in C:\wamp64\www\testprojet\app\Http\Controllers?
I suspect there isn't, and if needed you can create it using the current Controller.php file in the Laravel repository.
I'm unsure why this is the case, but suspect it may be due to a step being missing when setting your project up... What steps did you follow to get Laravel up and running for your project?

The error might be due to I have faced the same error because I have moved the Controller.php file from http/controllers folder to other place by mistake.
you should check that Controller.php file exists in the app/http/controllers.

Related

Laravel : command php artisan route:list generates an error : AuthController not found

I am working in a laravel project running version 5.6.39. When I try to run a command php artisan route:list, it generates an error saying "ReflectionException : Class App\Http\Controllers\Auth\AuthController does not exist".
routes\web.php looks like...
How can I resolve this issue? Please help.
Thank You.
The reason behind this issue is when you run php artisan route:list laravel reads all routes from routes folder and executes every controller that exists in them and if laravel couldn't find the controller or there is an error in them laravel throws an exception
so with that in mind it means you either removed App\Http\Controllers\Auth\AuthController or you moved it to another folder but didn't update the AuthController namespace and also the namespace in web.php so in your case you need to fix namespace in web.php and also in controller until they match

Controller does not exist using Laravel 5.6

I created the Laravel project and I uploaded it into a shared server. Inside the server, I tested the application some controllers are not working but in the local, I tested it's working without issues.
When I tested the application in server it's saying
ReflectionException (-1)
Class Asset_Management_System\Http\Controllers\SublocOneController
does not exist
I don't know what was the issue please help me to fix this issue.
Controller File
namespace Asset_Management_System\Http\Controllers;
use Illuminate\Http\Request;
use Asset_Management_System\MainLocation;
use Asset_Management_System\SubLocationOne;
class SubLocOneController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
}
Web.php file
Route::resource('SubLocationOne','SubLocOneController');
Try to connect via ssh to the server and run in the root of the site:
composer dump-autoload
Are u renaming the app file in laravel structure with Asset_Management_System ?
basically if u make controller with this path :
App\Http\Controllers\[YourControllerName];
1.1 just do this :
namespace App\Http\Controllers;
In other case, you can use composer dump-autoload in terminal inside your directory.
Typo SublocOneController and SubLocOneController?

Laravel: Routing & Controller

I'm a beginner using this laravel framework. Currently i'm trying to understand the routing and controller of this framework.
I created a controller file using this command:
php artisan make:controller Admin/PostController
Of course the output of this is to create a controller file inside Admin folder. Inside of the PostController.php i wrote a code like this:
public function create()
{
return view('admin.post.post');
}
Also, in my web.php i have this code.
Route::get('/', function () {
return view('user.blog');
});
Route::get('posts',function(){
return view('user.posts');
})->name('posts');
Route::resource('admin/post','Admin\PostController');
The "admin" is a folder and the "post" is a folder too inside the "admin" and when you open the "post" folder you'll see the "post.blade.php" file. The other two route::get in my web.php are working fine. But the route:resource is not working.
When i tried to run this in my browser using this link:
localhost:8000/admin/post/create
The browser says: Sorry, the page you are looking for could not be found
Is there any problem with syntax or path structures?
Here's my post.blade.php
#extends('admin.layouts.app')
#section('main-content')
this is just html codes..
#endsection
Here's my php artisan route:list
this because some time composer stuck, for that times you should stop serve and re generate autoload
1.stop your php artisan serve
2.enter this command
composer dumpautoload
3.run your php artisan serve

Laravel 5.4 internal server error while loading views from sub directory

I am new to laravel and just installed 5.4 version using composer.I created a new folder inside resources/views/pages and put a file named index.blade.php inside it. I am getting 500 internal server error while loading it inside the controller. I am unable to figure the problem out. the route I am using is:
Route::get('/','HomeController#index');
If I put index.blade.php inside views directory and use view('index');
there is no problem loading this. here is the directory strecture that I am using
HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index(){
return view('pages.home.index');
}
}
pages.home.index means that it would try to read a file called index.blade.php from the resources/pages/home directory.
So to solve your problem create the "home" directory inside of "pages" and move the index.blade.php there.
The other example works properly because view('index'); reads the file called index.blade.php from the resources directory.

Laravel 5.1 "ReflectionException in Container.php line 737:..class does not exist"

I'm sure that the solution to this problem is staring me right in the face, but unfortunately I can't seem to figure it out. I'm trying to add a route to my laravel 5.1 installation, and I'm getting the below error...
ReflectionException in Container.php line 737:
Class App\Http\Controllers\Tools\DashBoardController does not exist
I first edited the routes file to include the below...
(file: app\Http\routes.php)
Route::get('dashboard', 'Tools\DashBoard#index');
Then I created the "Tools" folder and "DashBoardController.php" file, and have it setup to look somewhat like the below...
(file: app\Http\Controllers\Tools\DashBoardController.php)
namespace App\Http\Controllers\Tools\DashBoard;
//...etc...//
class DashBoardController extends Controller { /* ..etc.. */ }
Is this all I have to do? I read that you could run "composer dumpautoload" in the terminal but unfortunately that didn't help.
I'm on a localhost XAMPP install w/PHP7 on Win7, if that's useful. Any help is greatly appreciated!
Your namespace declaration should look like namespace App\Http\Controllers\Tools and should not contain the filename or name of your class. Then you need to change your route to point to the name of your class Route::get('dashboard', 'Tools\DashBoardController#index');.
The way autoloaders and namespaces work in PHP and specifically in Laravel is that the namespace must reflect the directory structure and the class name must match its filename.
If you will have multiple routes using controllers from the same namespace, you will probably benefit from implementing route group namespaces.

Categories