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.
Related
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
I used Symfony version v4.4.1 . I create project in CLI using symfony command:
symfony new my_project_name --full
But my folder in src/Controller is empty, please, help.
Maybe my Controllers are created in another directory. On screenshot, you can see trouble.
Early, it's working nice.
When you create a new application Controller directory is empty. It's a normal result when you use the full argument. Full will only specified that you have all common utilities/libraries (twig, security, etc). But you have to create your own business (entities, controllers, forms, views, etc).
If you want to create a Controller, you should install the maker bundle
symfony composer req maker --dev
Then the console will be able to create Controller as easy as:
symfony console make:controller MyNewController
Default controller is no longer in the empty application, because some applications do not need them. As example, if you are developing an API, you do not use controller. Another example, some developers do not use MVC(model view controller) pattern but the ADR (Action Domain Responder) Pattern
I'm a bit confused about where something like this belongs to Laravel.
I want to write a web service client wrapper in laravel, and I want to access it like this:
\MyWSClient::getSomeInfoAbout($someId);
then the code will connect to the web service to http://www.someapi.com/api/getSomeInfoAbout?id=$someid&type=json with OAuth2 or some token requests, then fetch the data, keep token information until it expires, refresh token if needed etc.
But where will I put the code? In the vendor directory as a new package? I'm moving this code from a computer to another computer except vendor,storage and node_modules folders, because they are huge and when I do this, I will have to move only one folder in the vendor directory. And I'll need to publish a package under development to composer if I want portability etc.
Is there any other way to do something like this?
I think I've found the answer.
First I needed to use jeroen-g/laravel-packager package to create a new package using artisan console. I could've done that by hand, but I didn't know the required files.
Second, I've created a new package in the packages folder with my desired class name.
Third, I've added the provider and the alias for the class in app.php.
After that, I've created a test method in my controller and wrote a route for that. I called the static method I've written in the SkeletonClass the packager created for me.
And it worked with some tweaking after creation.
I've used php artisan packager:new tpaksu mypackage --i command for an interactive package creation which is cool.
Note: I've just learned the existence of this package, I'm not advertising it :)
I have got a project on its middle way. The person use something like this to create
a directory structure in the app folder:
php artisan myProject:plugin --create="sample"
And then this command creates a directory structure. This directory is like this:
./app/
plugins/
models
controller
views
migrations
Now, I don't know how should I make news migrations or migrate the current migration files
which I have applied some changes to. What should I do?
Thanks in advance
If your application is already in production, you must always create new migrations, because you cannot rollback migrations in production or you will loose data. So create a new migration for every little modification you need to do in your database tables.
Is there a general place that I could put logic that can be shared between the controllers and commands in Laravel. I have functionality that will most often times be run from command line via stored procedure, but also need the same (or a subset) of the functionality via web.
Can I use controller logic within the command? Or call the command from the controller/route?
Or should I just build my own classes and include them as needed in both?
just create a app/libraries folder. In that create a custom_helpers.php file and autoload it. Store your methods that your going to be using often there. Think of it like the helper class provided by laravel by default.