I'm starting out learning Laravel, and all the tutorials show how to use
php artisan make:controller BlahController
to make a new controller.
Is there a requirement I do this from the command line? If I want to do it by hand, what would I need to do to replicate that action?
I know that of course, I'd need to manually create the BlahController.php file inside the app\http\Controllers folder.
But does artisan make:controller alter/create any other files?
Broader question. Is php artisan required at all to work in Laravel? Or can all development be done by hand coding?
No, it is not required at all to use php artisan to create any files.
The purpose of artisan is to make your work more automated or less coding in general, but you are 100% correct, you can just manually create the controller file on its own (or model or view or anything related to laravel) without using artisan.
Example of artisan is to do things like:
php artisan create:model MyModel -all
This will create:
migration
seeder
factory
policy
resource controller
form request classes for the model
all of that is created and ready to be edited in a single command rather than having to manually create each file and filling it up and checking if the names are correct etc...
You can do so many things with artisan to simplify the process, and you can always read more on what options you have when creating things by doing:
php artisan create:controller -help
More of the documentation can be found here: https://laravel.com/docs/9.x/artisan#introduction
Laravel artisan helps you to do things faster
Ofcourse you can create it manually but artisan makes your life a little bit easier!
I highly recommend it
Also It's not about just creating a controller
It does a lot, read more about it here:
https://laravel.com/docs/9.x/artisan
Related
after these commands
php artisan make:model 'FileName' -mcs
Laravel make command files sources (Model, Controller, Migration, Seeder, Factory etc...)
How all basic files generate and where from these files comes?
All the generated stuff in Laravel use templates
If you run artisan command in your console, you can observe that exists a section called stub, and the only command in this section is php artisan stub:publish.
If you run that command it will generate a new folder inn your app root folder called stubs with a bunch of files inside all with extension .stub.
You can open those files and modify then or customize them as needed. From now on this folder will be the place from where your Laravel app will read the template for making all kind of stuff that artisan usually does.
This templates are included with every Laravel installation and is totally optional publish them or not. In fact there are many packages dedicated to make custom Controllers or Models like this one from Spatie
The internals above this generators
Laravel has two kernels,
The first one in app/Console/kernel
The second one in app/Http/kernel
When you run artisan, Laravel Bootstrap the app, and run the Kernel console. This both Kernels has different purposes, really they function as separates apps.
About the specific generation of the above files, I mean different controllers, Models, migrations etc.. all that stuff related to models are generated by one Class.
class ModelMakeCommand extends GeneratorCommand{ .... }
Which is located under Illuminate\Foundation\Console namespace.
You can check the code of that class and see how the stubs files are used to generate the variety of commands only related to Models, but there are many more, like Policies, Events, Jobs etc...
I hope this helps and answer your question
Here you are more information about this subject from Laravel News
These files are being generated from stub files. Here are some stubs directory location on any laravel project. you can check this out.
For Model :
vendor/laravel/framework/src/Illuminate/Foundation/Console/stubs/model.stub
Others :
vendor/laravel/framework/src/Illuminate/Foundation/Console/stubs
vendor/laravel/framework/src/Illuminate/Routing/Console/stubs
vendor/laravel/framework/src/Illuminate/Database/Console/Factories/stubs
vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/stubs
if you want control over these stubs you have to apply below command
php artisan stub:publish
this command will publish stub files on "stubs" folder on your project directory. Then you can customize according to your need.
I'm developing a webapp with Laravel and MongoDB (jenssegers/laravel-mongodb).
While creating a new model with php artisan make:model, the command uses Illuminate\Database\Eloquent\Model declaration in the file and every time I need to replace Illuminate\Database\Eloquent\Model with Jenssegers\Mongodb\Eloquent\Model manually.
Is there a way to automate the process?
It doesn't look like the package provides an Artisan command to create a MongoDB model stub, which seems like a bit of an oversight. However, it's not terribly hard to create this kind of generator command for Artisan yourself if you need it.
The model make command is at https://github.com/laravel/framework/blob/5.7/src/Illuminate/Foundation/Console/ModelMakeCommand.php and the stub file used to create it is at https://github.com/laravel/framework/blob/5.7/src/Illuminate/Foundation/Console/stubs/model.stub. If you extend the command class to replace the stub file with your MongoDB version, and amend the stub file to be a MongoDB model, then you should be able to create a command for generating MongoDB models. It might even be worth forking the package to add this and submitting a pull request to get it added to the package. I would refer to the part of the Laravel documentation that deals with Artisan for more details, as that describes the process of adding your own Artisan commands in detail.
Another approach would be to write your own class generator and then overwrite the command make:model
Add the following in the file routes/console.php to override the command
use Path\To\Class\MyCustomClassGenerator;
Artisan::command('make:model', function(){
new MyCustomClassGenerator();
$this->comment('new MongoDB Model generated');
});
I used cakephp before and it comes with bake command tool that generate full MVC skeleton with complete crud functions.
Cakephp's bake console just need database to read and it automatically generate the code with command:
bake all --everything
I want to know dose atrisan have the same tool or not? And if so how can I do that?
Should I install third party packages to achieve this or Laravel artisan support by default?
I prefer laravel default tooling if its available.
Thanks in advance.
I don' think there is a built in command like bake all in laravel. However this library seems to do what you are asking for
You can use artisan to create individual MVC components. Perhaps you could collect these in a shell script and run them all at the same time
Creating a controller
This will also generate the crud functions
php artisan make:controller ControllerName
Creating a model
php artisan make:model ModelName
I need to add service layer in my project where controllers are doing all work now.
Can I use PHP artisan to make service files, as we do for controllers: php artisan make:controller ?
How do you add service files?
I'll leave an answer just in case someone is also looking for it.
No, there is not such thing as service layer out of the box in Laravel, you should either implement it yourself or you can require a repository which implements it https://github.com/TakeoO/laravel-service-layer
How do you create a mysql view using php artisan? Can't find any documentation on it???
Google and Bing aren't returning much at all!
documentation for Laravel: http://laravel.com/docs/migrations
Your question is a little unclear. It seems you may not understand the difference between Views, Migrations and MySQL Tables.
Migrations can be created with Artisan via php artisan make:migration ... or manually. They contain code that create MySQL Tables.
Views are template files that display a page for end users to interact with. In Laravel, Views are Blade Templates.
Artisan does not offer a command for creating Views. But there are Composer packages you can install that will extend Artisan with such functionality.
https://github.com/bencomeau/artisan-make-view
https://github.com/svenluijten/artisan-view