I have this great doubt, if I copy and modify an existing controller in my laravel project vs create a controller with php artisan make:controller MyController, Is there any affected configuration files? or is it normal to copy and edit my other controllers?
It's just a lot easier to do the official command
php artisan make:controller myController
I mean, it's what they are there for. I don't believe that hand-making a controller will have a negative effect but it's so much easier and safe to use the function that's already defined.
many times in a controller you have a lot of logic that you want to reuse, IMHO sometimes it's worth manually copying the controller.
Related
In Laravel, what's the difference between a regular controller and resource controller? Please provide examples to illustrate the differences.
There is mainly no difference..It a special type of controller.
When you create a controller like this
php artisan make:controller YourNameController --resource
it auto create some function like index, create, store, show, edit, update, destroy. basically for crud.
For details go to documentation https://laravel.com/docs/7.x/controllers#resource-controllers
Well, the only difference is this.
The resource controller creates a PHP file with already defined CRUD operations while a regular controller creates an empty file.
To create a regular controller with the name UserController
php artisan make:controller UserController
To create a resource controller
php artisan make:controller UserController --resource
Hope it was helpful
Use this for more resources https://laravel.com/docs/9.x/controllers
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
I am trying to put laravel database migrations in sub folders, but not sure how to do this.
When creating new migrations and executing your migrations, you can pass in a path parameter through the command line interface to specify the directory it will use to create and run the migrations respectively.
php artisan make:migration create_users_table --path=/path/to/your/migration/directory
php artisan migrate --path=/path/to/your/migration/directory
In AppServiceProvider, find a boot method and add there following
$mainPath = database_path('migrations');
$directories = glob($mainPath . '/*' , GLOB_ONLYDIR);
$paths = array_merge([$mainPath], $directories);
$this->loadMigrationsFrom($paths);
Now you can use php artisan migrate and also php artisan migrate:back when having migrations in sub folders
If you eager to do this, here is a quick solution at nscreed/laravel-migration-paths
pakcage. Very simple and easy to use.
During the periodical development phase the migrations folder may become very large. It is very helpful if we can organize the content of the migration folders. This library helps to organize migration files in different folders. Even, if your organize your existing files it will works as well.
Hope, it will help you.
You might want to take a look at this package, it's not exactly what you are looking for but it is an alternative to sorting and organizing sections of your application including controller, migrations, models, views and others.
Nwidart's Laravel Modules for modular application development with Laravel,
It helps to organize your application into modules, and so, you can create migrations in each module, and since the modules are in separate folders it helps fix this issue someway.
In Laravel, I can create models and controllers automatically from the command line using
artisan
However, There is no command to generate views automatically based on the the model for CRUD operations. Wonder if there is a tool that would make this out of the box. I imagine this:
php artisan make:view model
and then I have all my views ready. I tried to search online but could not find a proper tool: LarvelCodeGenerator, and others...
Have you taken a look at Laravel View Generator.
I'm trying to make a resource controller in my package but i'm not sure how to do it.
Normally I would use:
php artisan controller:make MyController
But how can I make it so this is created in my package?
If you take a look at php artisan help controller:make you will see that there is a path option, so I guess that's what you're looking for.
Another option would be to explore the workbench which will make things easier while developing a package