Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use artisan in Laravel

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use artisan in Laravel". In daily operation, I believe many people have doubts about how to use artisan in Laravel. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use artisan in Laravel". Next, please follow the editor to study!

Artisan is the name of the command line tool included in Laravel, which provides some commands that are helpful to application development; artisan is driven by powerful Symfony Console components, and developers can use the list command to view all available Artisan commands.

This article operating environment: Windows7 system, Laravel5.7 version, Dell G3 computer.

What is artisan in Laravel?

Detailed explanation of the use of artisan tools (commands) in laravel

The artisan tool, first of all, this is a php file, which is placed in the root directory of our laravel framework

Brief introduction of Artisan tool

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

Execute php artisan list in the script to view all the commands

Those who are familiar with linux know that no matter what command there is, there is a help command. When running, all of a sudden, if you forget, you can execute the help command to check the commands we need. Similarly, in the laravel framework, you can also execute the help command to check such as:

Php artisan help migrate

Using artisan tool to create Controller

Students who understand the tp framework will know that if you want to create a controller, you need to write your own handwriting, namespaces, inheritance 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 an extra MsgController, and it is ready to add, delete, change, check, etc. What if you don't need it? It is very simple, one is to delete directly, and the other is to use commands to generate blank controllers such as:

Php artisan make:controller-plain

However, some students will encounter this situation, for example, a project is divided into foreground and background, the corresponding controller also needs to be placed in a different directory, how to deal with this, how to use the artisan tool? 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 and find that the namespaces are sorted out automatically.

This command indicates that the flexibility of the laravel framework is verified, and developers are free to specify it.

Using artisan tool to create model

As programmers, when developing a project, we need to manipulate the database, so it must have something to do with model, so how to use artisan tools to create a model? More or less the same as the controller, such as creating a model for Msg

Php artisan make:model Msg

In laravel, it is placed under the App directory by default. If you want to define your own folder, it is the same as the controller. You can specify its directory, but it is important to note that the namespace needs to be changed.

View routes with artisan tool

In the laravel framework, if you want to respond to the methods in the controller in the browser, you just need to specify the route in App\ routes.php, but if we use resource routing, you only need to specify a route, and it also generates a lot of methods directly, but we can't see it, so what should we do?

Php artisan route:list

Use this command to view all routes

Using artisan tools to create Middleware

First of all, middleware is also a very convenient thing in laravel, so what is middleware?

The simple understanding is that the middleware can be executed before or after the method of our controller to affect the result of execution.

Anyone who has done a project in the laravel framework knows that every time post submits, it will report a Token error. The solution is to add a

{! csrf_field ()!!} is fine, but what is the root cause?

In the case of app\ http\ middleware\ VerifyCsrfToken.php, post this middleware globally and verify it whenever you submit it to post.

How to generate a middleware?

Php artisan make:middleware EmailMiddleware

The execution will be found under the middleware folder.

At this point, the study on "how to use artisan in Laravel" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report