Home > Backend Development > PHP Tutorial > Laravel 514 + Bootstrap 334 Note 4: Laravel Controller

Laravel 514 + Bootstrap 334 Note 4: Laravel Controller

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-30 13:31:41
Original
981 people have browsed it

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(&#39;hw&#39;);
    }
}
Copy after login

Create a new view hw.php under resources/views, The content is as follows:
<html>
<body>
Hello World!
</body>
</html>
Copy after login

Modify routes.php. The modified code is as follows:
<?php

Route::get(&#39;/&#39;, function () {
    return view(&#39;welcome&#39;);
});

Route::get(&#39;/hw&#39;, &#39;HomeController@hw&#39;);
Copy after login

Open the browser and visit: http://localhost:801/hw, as shown below:


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(&#39;hw&#39;,[&#39;name&#39;=>'CBW']);
    }
Copy after login
Modify the hw.php view page code:
<html>
<body>
[<?php echo $name; ?>],您好!
</body>
</html>
Copy after login
visited again as follows:


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');
Copy after login
Modify the hw method of the controller:
    public function hw($name)
    {
        return view('hw',['name'=>$name]);
    }
Copy after login
and then visit again: http://localhost:801/hw/calvin, as shown below:


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(&#39;Visit.Home.hw&#39;,[&#39;name&#39;=>$name]);
    }
}
Copy after login

D. Modify the routes.php code segment as:

Route::get('/hw/{name}', 'Visit\HomeController@hw');
Copy after login
E. Move the view file hw.php to resources/views/Visit/Home;
Now, visit again: http://localhost :801/hw/calvin, is still correct.

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;  
}]);  
Copy after login

In fact, we can also process it in the constructor of the controller:
class UserController extends Controller {
    public function __construct()
    {
        $this->middleware('my');
    }
}
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template