Home > PHP Framework > Laravel > What is artisan in Laravel

What is artisan in Laravel

藏色散人
Release: 2022-01-06 14:37:08
Original
2732 people have browsed it

artisan is the name of the command line tool that comes with Laravel. It provides some commands that are helpful for application development; artisan is driven by the powerful Symfony Console component. Developers can use the list command to view all Available Artisan commands.

What is artisan in Laravel

The operating environment of this article: Windows 7 system, Laravel version 5.7, Dell G3 computer.

What is artisan in Laravel?

Detailed explanation of the use of artisan tool (command) in laravel

artisan tool, first of all, this is a php file, which is placed at the root of our laravel framework Table of Contents

  • Artisan Tool Introduction

Artisan is the name of the command line tool that comes with Laravel. It provides some commands that are helpful for your application development. It is powered by the powerful Symfony Console component. In order to view all available Artisan commands, you can use the list command to list them:

Execute in the scriptphp artisan listYou can view all commands

Then Those who are familiar with Linux know that no matter what command there is, there is a help command. When running, suddenly, if you forget, you can execute the help command to check the command we need. Similarly, you can also execute help in the laravel framework. Command to view such as:
php artisan help migrate

  • Use artisan tool to create a controller

Understand tp Framework students will know that if you create a controller, you need to handwrite, namespace, inherited classes, etc.
Then in laravel, you can also use artisan tools to execute, such as creating a MsgController
php artisan make:controller MsgController
After execution, 'app\http\Controllers' will have one more MsgController, and methods are also prepared to add, delete, modify, check, etc. What if you don't need it? What? It's very simple. One is to delete it directly, and the other is to use commands to generate a blank controller, such as:
php artisan make:controller --plain

However, some students will encounter In this case, for example, a project is divided into frontend and backend, and the corresponding controllers also need to be placed in different directories. How to deal with this? If you use artisan tools? For example, create an Admin\GoodsController

php artisan make:controller Admin\GoodsController
The premise is that the Admin folder has been created under App\Http\Controllers in the laravel project. After execution, you will find that there are more GoodsController files under admin. Open it , it is found that the namespace is also automatically organized;
This command instruction verifies the flexibility of the laravel framework, developers can specify it at will;

  • Use the artisan tool to create the model

As programmers, while developing projects, we need to operate the database, which must be related to the model. So how to use the artisan tool to create a model? And controller, Much the same, such as creating a Msg model

php artisan make:model Msg
In laravel, it is placed in the App directory by default. If you want to define your own folder, it is the same as the controller. We specify it. directory, but it should be noted that the namespace needs to be changed

  • Use the artisan tool to view the route

In the laravel framework, If you want to respond to the method in the controller in the browser, then you only need to specify the route in App\routes.php, but if we use resource routing, we only need to specify a route, and it Many methods are also directly generated, but we cannot see them, so what should we do?
php artisan route:list
Use this command to view all routes

Use artisan tool to create middleware
First of all, in laravel, middleware is also a very convenient thing, so what is middleware?
The simple understanding is that middleware can be placed in our controller. Execute before or after to affect the execution result.
Anyone who has used the laravel framework and done projects knows that every time a post is submitted, a Token error will be reported. The solution is to add
{ !!csrf_field()!!}, but what is the root cause?
In the problem of app\http\middleware\VerifyCsrfToken.php, send this middleware globally, every time It will be verified when submitting the post;

How to generate a middleware?

php artisan make:middleware EmailMiddleware
The execution will be, Find

in the middleware folder. The latest five Laravel video tutorials (recommended)

The above is the detailed content of What is artisan in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template