Laravel 514 + Bootstrap 334 Note four: Laravel controller

Source: Internet
Author: User
In the previous article, we put the user's request response logic in the routing process, in fact, this is not realistic, and there is no front code so simple.

In most cases, the user's request action is handled in the controller (this does not include the business processing logic).

All controllers in the Laravel are in the App/http/controllers directory.

1 Creating a simple controller

1.1 Controller with no parameters

Create a new file homecontroller.php in the directory app/http/controllers directory, with the following code:

 
Under Resources/views, create a new view hw.php with the following content:
Hello world!

Modify the routes.php, and the modified code is as follows:
  
Open browser access: HTTP://LOCALHOST:801/HW, as shown:


1.2 Controller passing parameters to view

When the controller needs to pass parameters to the view, this is the way to modify the controllers ' HW method:

    Public function hw ()    {        return view (' HW ', [' name ' = ' CBW ']);    }
To modify the Hw.php view page code:
[
 
     ], Hello!
Visit again as follows:


1.3 The controller reads the parameters from the route and passes

When the controller needs to get the parameters from the route, modify the route snippet shown in the example above:

Route::get ('/hw/{name} ', ' HOMECONTROLLER@HW ');
To modify the controller's HW method:
    Public function HW ($name)    {        return view (' HW ', [' name ' = = $name]);    }
Then visit again: Http://localhost:801/hw/calvin, as shown:


2 Router Depth

2.1 Controllers and namespaces

In general, an application system will be composed of a number of sub-projects, such as a site has a front desk and backstage, the front desk has news features for reading, the background has news features for management.

Now, let's suppose to develop a web system that consists of two modules: the Common user module (Visit) and the System Management module (Manage).

A. Create a new controller subdirectory for two modules in the App/http/contollers directory: Visit, Manage;

B. Create two subdirectories under Resources/views: Visit, Manage, and create subdirectories under Visit: Home;

C. Move the HomeController created in the previous example to the new Visit in the previous step. The modified code is as follows:

 
    $name]);}    }

D. Modify the Routes.php code snippet to:

Route::get ('/hw/{name} ', ' VISIT\HOMECONTROLLER@HW ');
E. Move the view file hw.php to the Resources/views/visit/home;
Now, visit again: Http://localhost:801/hw/calvin, still the right one.

2.2 Controller Middleware

In the previous article we have demonstrated the use of middleware, and then review the example above:

Route::get ('/user/{age} ', [' middleware ' = ' my ', function ($age) {      return ' user age: '. $age;  }]);  

In fact, we can also handle it in the constructor of the controller:
Class Usercontroller extends Controller {public    function __construct ()    {        $this->middleware (' my ');}    }

In addition, there are implicit controllers, RESTful, routing caches and so on, and then continue to add.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the Laravel 514 + Bootstrap 334 Note Four: Laravel controller, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.