This article describes the basic usage of Laravel5.1 framework controller. Share it for your reference, as follows:
Why use a controller
For example, when we wrote some logic in Route (routing), the Route file is particularly huge. In fact, we should draw all these logic into a controller, and after routing and distributing it to the controller, the controller does the corresponding operations. For example, the logic about the background should be drawn into the AdminController, and the Route file is only distributed.
1 How to create a controller
1.1.1 Creating a RESTful controller
As for what is RESTful? Baidu - -, let me briefly talk about it first. It is automatically filled with some methods of adding, deleting, modifying and checking. OK We create it in the Artisan console:
php artisan make:controller Admin\\HomeController
Then we find it under app/Http/Controller/Admin/:
class HomeController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // } }
Each method is its literal meaning. As for how to use it, we will register a route and it will be clear.
1.1.2 Implementing RESTful routing
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function (){ Route::resource('/', 'HomeController'); });
Resource is to register multiple routes for RESTful style controllers. We can check it out in the Artisan console:
php artisan route:list
If there is no error, we will definitely see the printed table, which indicates the properties of each route. If you still don’t understand it, it doesn’t matter~ Our future articles will frequently use these contents.
1.2.1 Normal controller
In actual development, sometimes we don’t necessarily need a RESTful-style server. We only want an empty controller to implement some methods ourselves, which can be generated like this:
php artisan make:controller UserController --plain
Just follow a plain identifier to generate an empty controller.
1.2.2 Using the controller for a single route
The above is to use the resource method to correspond to the controller's actions. How do we use empty controllers like this in routing?
/** * Controller name @ method name */ Route::get('/user', 'UserController@index'); /** * This is how to write when configuring some other things, because it needs to be in an array, its corresponding KEY is uses. */ Route::get('/user', ['as' => 'showUser', 'uses' => 'UserController@index']);
Then implement the index method of UserController by yourself:
class UserController extends Controller { public function index() { return 'Show something'; } }
1.3.1 In fact, there is another way to write the implicit controller
I don't use this kind of route very often. Let's take a look at the syntax first:
Route::controller('/user', 'UserController');
First, the controller is used to register the route. The first parameter is that the path is not new, and the second parameter is the controller, but it does not follow the method. Because it can automatically correspond to the method according to the path, look at the implementation of the controller method below.
class UserController extends Controller { /** * Corresponding to /user/ routing GET method */ public function getIndex() { return 'Show something~'; } /** * Corresponding to /user/show/{id} routing GET method */ public function getShow($id) { return 'This user ID is: '.$id; } /** * Corresponding to /user/update/{id} routing POST method */ public function postUpdate($id, Requests\Request $request){ } }
The methods of this controller start with HTTP requests. The most commonly used ones are followed by GET and POST. The method name is followed by the method, and the specific corresponding paths are clearly written in the comments.
If you want to name these routes, you can name them with an array:
Route::controller('/user', 'UserController',['getShow' => '']);
This way, you can name the corresponding method. Oh funk Big K really doesn't like this method. Maybe he doesn't understand the true meaning of writing like this.
--I will write so much about the controller today. We have to learn some new knowledge to combine the specific logic implementation, such as middleware, request, and blade template engine, but it is not very difficult.
For more information about Laravel, readers who are interested in this site can view the topic:Laravel framework introduction and advanced tutorial》、《Summary of excellent development framework for php》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope that this article will be helpful to everyone's PHP programming based on the Laravel framework.