The use of Laravel framework controller

1. Controller use (C)

The main role of the controller is responsible for receiving user input requests, scheduling the model to process the data and finally using the view to display the data.

1. Where is the controller file written?

Insert picture description here
Among them, Auth stores the sample controller files related to Auth authentication that comes with the framework. The controller.php file is the framework's base controller. The existence of the Auth folder shows that the controller can be managed by directories (modules). .

2. How to order the controller file?

The naming can refer to the sample file given in the Auth folder:
Insert picture description here
File naming method: Big Hump+Controller.php.

3. How to write structure code?

Note: The controller structure code does not need to be manually written by yourself, and can be automatically generated by the artisan command.
as follows:
php artisan make:controller 控制器名
Insert picture description here

php artisan make:controller 目录路径/控制器名(If you have sub-directories)
Insert picture description here
Effect:
Insert picture description here

4. Controller routing (the project is mainly based on this method)

That is, how to use routing rules to call methods under the controller instead of callback functions.
The routing setting format is basically the same, except that the anonymous function is replaced by "controller class name@method name". The
definition format is as follows:
Route::Request method ('route expression','controller@method');
For example: in My control Create the my1 method in the browser, which outputs phpinfo information.
Insert picture description here
Insert picture description here
Effect:
Insert picture description here
If the page reports an error:

laravel 报 Target class [MyController] does not exist.

Then look in the project \app\Providers\RouteServiceProvider.phpto find protected $namespace = 'App\\Http\\Controllers';lift comment on it.
Add it if you don't find it.


Use sub-directory management to control its routing:
Insert picture description here

Insert picture description here
effect:
Insert picture description here

5. Receive user input [Key points]

The class that receives user input: Illuminate\Support\Facades\InputThe version above laravel8 is used Request, and the usage is similar.
Facades: The idea of ​​"facade". The facade is a state between instantiation and no instantiation of a class. In fact, it is an interface implementation of the class. In this state, the class can not be instantiated but the methods in the class can be called. To put it bluntly, it is the invocation of static methods.

Input::get('The name of the parameter','If the parameter is not passed, use the default value') // Similar to the ternary operator in PHP
Input::all() // Get all user input
Input::get ('Parameter name') // Get the input of a single user
Input::only(['id','age']) // Get the input of a few users
Input::except(['id','age ']) // Get all the parameters other than the input of a few users
Input::has('name') // Determine whether a certain input parameter exists
(the above can get the information in the get, or get the post Information)

In Laravel if it requires the use of facades, but do not write such a long pull-in operation:
may (alias aliases defined array) in the config / app.php long strings defined alias:
Insert picture description here
Now enter the following code:
in web.phpthe Define controller routing:
Insert picture description here
It can also be used in the laravel framework dd(需要打印的内容)(dd = dump + die), but the content after the dd function will not continue to execute, and the subsequent code of dump can also be executed.
We TestControllernext enter the following code to test:
Insert picture description here
results are as follows:
Insert picture description here
More rules can click here.

On the way of learning php, if you think this article is helpful to you, then please pay attention to like and comment Sanlian, thank you, you must be another support for my blog.

Guess you like

Origin blog.csdn.net/weixin_44103733/article/details/113574899