: This article mainly introduces Laravel514 + Bootstrap334 Note 4: Laravel controller. if you are interested in PHP tutorials, refer to it. In the previous article, we put the user's request response logic in the routing for processing. in actual situations, this is not realistic and it is not as simple as the preceding code.
In most cases, user request operations are processed in the Controller (this does not include the business processing logic ).
All Laravel Controllers are in the app/Http/Controllers directory.
1. create a simple controller
1.1 no parameter controller
Create the HomeController. php file in the app/Http/Controllers Directory. the code is as follows:
Create a new view hw. php under resources/views with the following content:Hello World!
Modify routes. php. the modified code is as follows:
Open the browser to access: http: // localhost: 801/hw, as shown in:
1.2 The controller transmits parameters to the view
When the Controller needs to pass parameters to the View, this is the case. modify the hw method of the Controller:
public function hw() { return view('hw',['name'=>'CBW']); }Modify the code on the hw. php view page:[
], Hello!
Access again as follows:
1.3 The controller reads and passes parameters from the route
When the controller needs to obtain parameters from a route, modify the routing code segment shown in the preceding example:
Route::get('/hw/{name}', 'HomeController@hw');Modify the hw method of the controller: public function hw($name) { return view('hw',['name'=>$name]); }Then access: http: // localhost: 801/hw/calvin again, as shown in:
2 router depth
2.1 Controller and namespace
Generally, an application system is composed of multiple sub-projects. for example, a website has a front-end and a back-end. the front-end has news functions for reading, and the back-end has news functions for management.
Now, let's assume that we develop a Web system that consists of two modules: Visit and Manage ).
A. create the sub-directories of the two modules in the app/Http/Contollers Directory: Visit and Manage;
B. create two subdirectories under resources/views: Visit and Manage, and create the subdirectory Home under Visit;
C. move the HomeController created in the previous example to the Visit created in the previous step. The modified code is as follows:
$name]); }}
D. modify the routes. php code segment:
Route::get('/hw/{name}', 'Visit\HomeController@hw');E. move the view file hw. php to resources/views/Visit/Home;
Now, it is still correct to access http: // localhost: 801/hw/calvin again.2.2 Controller middleware
We have demonstrated the use of middleware in the previous article. let's review the above example:
Route: get ('/user/{age}', ['middleware '=> 'My', function ($ age) {return 'user age :'. $ age ;}]);
In fact, we can also process it in the controller constructor:class UserController extends Controller { public function __construct() { $this->middleware('my'); }}In addition, there are implicit controllers, RESTful, and route caches, which will be supplemented later.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces Laravel 514 + Bootstrap 334 Note 4: Laravel controller, including some content, and hope to help those who are interested in PHP tutorials.