Target class [UsersController] does not exist - laravel

I am trying to load up a view, as simple as that. I don't know where to go I using composer dump-autoload that it was an issue with loading the controller however it didn't help.
The pictures show the project filesRoutes file
Controller
View
Error
I'm hoping to get some help!

If you are using laravel 8 you should pass controllers in routes in new way. This syntax which you try is the old one. Check this..
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);
Also you should follow convention and name your controllers in singluar form. user not users. Let me know if it helped.

Related

How to avoid overwriting routes in Laravel?

Sorry in advance, I know it has been asked before but I did not figure out the solution.
I am new to Laravel, still learning and stuck with this issue:
My objective is to add pages in admin and show these pages in frontend.
For the Front part of the website I have this route:
Route::get('/{page}', 'PagesController#show');
so the when you access /about, /contact, /another-page I use the same view
For the Admin part of the website I have this route:
Route::get('/admin', 'AdminController#show');
My problem is that the first route overwrites the second route and I don't know how to avoid this.
I have tried with namespaces and grouping routes, but I get the same outcome.
Thank you
To make it simple this is happening because you have the route with the parameter before the admin route so is going to send the "admin as a parameter of page"
The Simple fix is just put admin route before your "/{page} so it will find admin route first,Something Like this:
Route::get('/admin', 'AdminController#show');
Route::get('/{page}', 'PagesController#show');
But I do not recommend building your routes this way and have specifics pages setup if possible, This way of building routes will mess around with the 404 route not found aswell.

Laravel Class Controller not found because route is weirdly case sensitive

I wanted to add a controller to an already existing project of mine. Wrote a controller file, added the link in the admin_api route file and adjusted the view I needed.
On my local machine everything works fine.
On production it failed (it's supposed to retrieve a collection, which is then handled and displayed by a vue js component) and the network monitor showed an error 500.
I ssh into the website and ran php artisan route:list which gave me the error
In Container.php line 779:
Class App\Http\Controllers\Admin\Api\StatsController does not exist
Although the file StatsController is in App\Http\Controllers\Admin\API\
After playing around I realized that the problem was with the folder name "API", as laravel was looking for "Api". So apparently it's case sensitive.
Placing the controller in a new folder "Api" solved the issue.
Moving all the other "API" controllers to "Api" broke their routes.
when running php artisan route:list and in RouteServiceProvider all routes appear as under Admin/Api.
What I don't get is where is this being registered? Is there a way to change this?
I find it quite annoying that the controllers sit now in separate folders and it seems quite absurd to me. I don't quite get how the original controllers worked if they seem to contradict the other configurations.
Working with Laravel 5.7
I would appreciate any insights!
Thanks
Just put all the relevant api controllers in a folder named Api or API (just pick a convention and stick with it), then make sure that each file's namespace (declared at the top) is correct and reflects the folder structure you have chosen.
For example, if your controllers are in app/Http/Controllers/Admin/Api, check that each controller file begins with:
<?php
namespace App\Http\Controllers\Admin\Api;
// Rest of code...
StatsController.php must have the namespace as App\Http\Controllers\Admin\API. In Controller.php you have to import as App\Http\Controllers\Admin\API\StatsController. After that, run composer dump-autoload

URL Routing: Laravel

I have been working on laravel and have been doing some routing. I was just wondering on what is the difference between writing the route as:
route::get('roles/{id}/edit',rolesController#edit);
versus
route::get('roles/edit/{id}',rolesController#edit);
One difference is clearly visible and that is the placement of the id variable. Can't figure out any other reason. Please provide an explanation on this.
Other than the actual look of the URL, there's no real difference as far as the framework is concerned.
I suppose it's the matter of preference when using any of this. Maybe, for example, if you are giving options of editing the user profile and posts, this might come handy as both are different routes, technically
No difference. It depends on you how you would like to build your routes. But try to user best practices which laravel creator recommend (https://laravel.com/docs/5.7/controllers#resource-controllers).
And also i want take your attention on how you called your controller. You should use CamelCase for naming your files (https://github.com/alexeymezenin/laravel-best-practices/blob/master/README.md).
There's no difference, but you might want to look in reosource routes and controller. Basically, laravel framework automatically creates routes and methods for controllers that you might need in your project. For example:
If you create a contoller like this:
php artisan make:controller RolesController --resource
and create a resource route like this:
Route::resource('/roles', 'RolesController ');
framework automatically crates this routes for you:
Verb Path Action Route Name
GET /roles index roles.index
GET /roles/create create roles.create
POST /roles store roles.store
GET /roles/{roles} show roles.show
GET /roles/{roles}/edit edit roles.edit
PUT|PATCH /roles/{roles} update roles.update
DELETE /roles/{roles} destroy roles.destroy
So you don't have to make your own routes and ask yourself if they are correct or not. Look into laravel official documentation for more info about this.

Laravel 5.4.36 - Deleted controller now having problems

I created a second controller in an attempt to imitate a PostsController I learnt off the Laravel From Scratch tutorial on YouTube in order to try and change the redirect folders in the public function etc to a different view's folder and see what happens but it was clashing with the original controller so I ended up deleting it along with its route::resource code.
Now when I try to go back to my original "Posts" page in the browser i get an ErrorException reading:
include(C:\wamp64\www\lsapp\vendor\composer/../../app/Http/Controllers/ShowsController.php): failed to open stream: No such file or directory
I imagine this is a composer issue and wondering if I could get some insight on how to fix it! Thanks
So I figured it out and all I had to do is run the composer dump-autoload command in the integrated terminal.
fixed!

Run two websites with one laravel installation

How we can use multiple applications with one Laravel installation.
For example:
www.apple.com
www.orange.com
both website will use ONE CORE laravel installation with separate database and all other separate stuff.
Is that possible?
If yes then how?
Also I have done the same thing for CodeIgniter but not sure how i can do with
laravel.
You can use route group for this, it's something like this:
Route::group(['domain' => 'myapp.com'], function () {
Route::get('user/{id}', 'Controller#Method');
});
Official documentation: Sub-domain routing
You can use this package https://github.com/nWidart/laravel-modules to better organize your code into it's separate websites within the same laravel base code. E.g. you can have Website1 and Website2 modules that has their own Controllers, Models, Views, database migrations, etc.

Resources