1. Controller usage (C)

The main role of the controller is to receive user input requests, schedule the model to process the data and display the data using the view.

1, where to write the controller file?

The controller. PHP file is the base controller of the framework. The presence of the Auth folder indicates that the controller can be managed by module.

2. How to command the controller file?

You can refer to the example files given in the Auth folder for naming:

File name: Big Hump + controller.php.

3. How to write the structure code?

Note: The controller structure code can be generated automatically using the artisan command, without having to manually write it. PHP artisan make:controller Name of the controller

PHP artisan make:controller Directory path/controller name(If you have a subdirectory)

Effect:

4. Controller routing (this mode is the main one in the project)

That is, how to use routing rules to call methods under the controller instead of walking through the callback function. Route:: Request method (‘ Route expression ‘, ‘controller @ method’); Route:: request method (‘ Route expression ‘, ‘controller @ method’); For example, create a my1 method in My controller that prints phpInfoEffect:

If the page error:

Laravel to Targetclass [MyController] does not exist.
Copy the code

So in the project, go to \app\Providers\ routeserviceprovider.php and find protected $namespace = ‘app\ \Http\\Controllers’; Just uncomment it. Add it if you can’t find it.


Control its routing using subdirectory management:

Effect:

5. Receive user input

Classes that receive user Input: Illuminate\Support\Facades\Input Laravel8 and above use Request ina similar way. The Facades. A facade is a state between an instantiation of a class and no instantiation. It’s actually an interface implementation of the class. In this state you can not instantiate the class but you can call methods in the class. Static method calls.

Input::get(‘ name of parameter ‘, ‘Use the default value if the parameter is not passed ‘) // Similar to the PHP ternary operator Input::all() // Get the Input of all users Input::get(‘ parameter name ‘) // Get the Input of a single user Input::only([‘id’, Input::except([‘id’, Input::has(‘name’) // Check whether an Input parameter exists.

If you want to use facades in Laravel, but do not want to write long intro operations, you can define a long string of aliases in config/app.php (define aliases in the array of aliases) :Next enter the following code: inweb.phpDefines the controller route in:It is also available in the Laravel frameworkDd (What needs to be printed)(dd = dump + die) But the content after the dd function will not continue to execute, dump the following code can be executed. We are inTestControllerEnter the following code to test:The effect is as follows:More rulesYou can click here.

On the way to learning PHP, if you find this article helpful to you, then please pay attention to like comment 3 times, thank you, your must be another support of my blog.