In the previous article, we put the user's request and response logic in routing. In actual situations, this is not realistic and is not as simple as the previous code.
In most cases, user request operations are handled in the Controller (this does not include business processing logic).
All Laravel controllers are in the app/Http/Controllers directory.
1 Create a simple controller
1.1 Controller without parameters
Create a new file HomeController.php in the directory app/Http/Controllers, the code is as follows:
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; class HomeController extends Controller { public function hw() { return view('hw'); } }
<html> <body> Hello World! </body> </html>
<?php Route::get('/', function () { return view('welcome'); }); Route::get('/hw', 'HomeController@hw');
1.2 The controller passes parameters to the view
When the Controller needs to pass parameters to the View, it is like this. Modify the hw method of the controller:
public function hw() { return view('hw',['name'=>'CBW']); }
<html> <body> [<?php echo $name; ?>],您好! </body> </html>
1.3 The controller reads parameters from the route and passes them
When the controller needs to get parameters from the route, it is like this. Modify the routing code segment illustrated above:
Route::get('/hw/{name}', 'HomeController@hw');
public function hw($name) { return view('hw',['name'=>$name]); }
2 Router in-depth
2.1 Controller and namespace
Generally, an application system will be composed of multiple sub-projects. For example, a website has a front-end and a back-end. The front-end has a news function for reading, and the back-end has news. Functions are used for management.
Now, we assume that we develop a Web system that contains two modules: the ordinary user module (Visit) and the system management module (Manage).
A. Create two new module controller subdirectories in the app/Http/Contollers directory: Visit and Manage;
B. Create two subdirectories under resources/views: Visit and Manage, and create them under Visit Subdirectory: Home;
C. Move the HomeController created in the above example to the Visit created in the previous step. The modified code is as follows:
<?php namespace App\Http\Controllers\Visit; use App\Http\Controllers\Controller; class HomeController extends Controller { public function hw($name) { return view('Visit.Home.hw',['name'=>$name]); } }
D. Modify the routes.php code segment as:
Route::get('/hw/{name}', 'Visit\HomeController@hw');
2.2 Controller middleware
In the previous article, we have demonstrated the use of middleware, let’s review the above example:
Route::get('/user/{age}', ['middleware' => 'my', function ($age) { return '用户年龄:'.$age; }]);
class UserController extends Controller { public function __construct() { $this->middleware('my'); } }
In addition, there are implicit controllers, RESTful, route cache, etc., which will be added later.
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces Laravel 514 + Bootstrap 334 Note 4: Laravel controller, including aspects of the content, I hope it will be helpful to friends who are interested in PHP tutorials.