Laravel Class Controller not found because route is weirdly case sensitive - laravel

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

Related

Livewire folder structure

I am quite new to Livewire and I feel a little bit confused about its folder structure.
I use the classic Laravel folder structure for my views and I put the components under the /resources/views folder; now with Livewire it seems that I am forced to put my components under /resources/views/livewire folder otherwise the component throws an error.
I also tried to change the view_path in my config/livewire.php file to resource_path('views'), instead of resource_path('views/livewire'), but it doesn't work.
The result is a messy structure like the following, where the users' components are in a different folder and I have to go back and forth to find what I want.
Wouldn't it be possibile to remove the /resources/views/livewire folder and put the components under the relative folder they belong to?
resources
views
users
index.blade.php
livewire
users
table.blade.php
the answer is the config file /config/livewire.php
'view_path' => resource_path('views/livewire'),
Just make sure you update all the Livewire class files views in their render function and that you clear any stale config information
php artisan cache:clear
edit: I have tested this and have regular Laravel blades along side livewire ones.

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!

Laravel File Manager 404 error

I just installed Laravel file manager on my project and I don't understand why I get an NotFoundHttpException when I try to pop up the file manager.
I've installed it in many projects and it's the first time that I get this kind of error.
I already put the LaravelFilemanagerServiceProvider in my providers,
and the service provider is above the RouteServiceProvider
I've tried to clear the route cache, to publish all files etc...
When I do php artisan route:list I have the correct routes with the correct middlewares,
but on my browser impossible to access the file manager,
so with the console the routes appear to be correctly registered, but the browser does not find it
any idea how to proceed ? Thank you for your answers
I found the solution,
I used a pre-made code for the wysiwyg editor coming from Laravel File Manager website but the code was wrong,
Be careful in the "Integration" section, the code is not always correct with the default given configuration.

External PHP Include in Laravel

We run a main website which is not made in Laravel. A specific script on this website however is made in Laravel. We need this script (or to be exact, a specific view inside of it) to fetch some resources from the main website (PHP files which mainly include HTML). When, inside the Laravel application, we try to include these resources, we can not - as they don't exist in the Laravel project workspace. This results in an error that the file does not exist. Attempting to climb out of the project (../../../file.php) does not seem to help either.
We're including it this way:
<?php
include '~/template/nav.php';
?>
We don't wish to include this file into the actual Laravel project, as that would require having to update it twice to ensure they remain equal. Is there any way for us to include this "external" file in our view? Every bit of research done seems to suggest adding it into the project, which would just cause twice the amount of administration on updates.
Cheers!
Just faced same problem. My Laravel Project is an API that need's to include outside php file. So what I did was specify entire path:
include($_SERVER['DOCUMENT_ROOT'].'/../../my_example_file.php');
$_SERVER['DOCUMENT_ROOT'] will lead you to Laravel's public folder.

Laravel showing blank page

I just setup a Laravel 5 framework in my server by typing in terminal
laravel new blog (in "/var/www/html/" folder),
then changed the default config of Nginx so the root pointing to : root /var/www/html/blog/public;
of-course all the files are in place however I currently just see a blank page showing up ! I tried to put a html & PHP file in public folder and it all works fine. but not Laravel default index file. what am I missing here ?
For adding pages in Laravel you do not simply put files in /public. Please have a look at the official Laravel page if you want to create new views.
Laravel uses the MVC principle. So you need to have at least a view (the part which is displayed) and a controller which handles the view. To add a new Controller, please change to the project root cd /var/www/html/blog and type in php artisan controller:make AwesomeController which creates the controller in app/Http/Controllers/AwesomeController.php.
In this Controller you can simply return a view by adding return view('myview') to the index() Method for example. (The view has to exist at resources/views/myview.blade.php of course)
In the last step you need to tell Laravel how to call your controller. Please modify the routes.php file at app/Http/routes.php and add Route::get('foo', 'AwesomeController');. Now you need to tell composer that some of your controllers may have changed and composer needs to refresh the cache. Type in composer dump-autoload and start the development server php artisan serve.
By calling http://localhost:8000/foo (by default) you should see your View. Have a nice day!

Resources