Laravel514 + Bootstrap334 Note 2: Laravel route

Source: Internet
Author: User
: This article mainly introduces Laravel514 + Bootstrap334 Note 2: Laravel routing. if you are interested in the PHP Tutorial, refer to it. 1. routing mechanism

In MVC, routing is a very important function and its role is:
A. match the input request and parameters included in the request based on the user access (URL;
B. call the Action method mapped to the Controller and pass in the parameters;
C. return the Action method processing result;
Represents a user request in a simple form:


2. routes in Laravel

In Laravel 5.1.4, the routing configuration file is app/Http/routes. php.

2.1 directly return a string route

Append the following code snippet to the original code:

Route::get('/hw', function () {    return 'Hello World';});
Open the browser to access: http: // localhost: 801/hw, as shown in:


2.2 route entries returned to the view

In the above code, append the following code segment:

Route::get('/home', function () {    return view('home');});
Create the view file home. php under the resources/views Directory. the content is as follows:
home
Open a browser and access: http: // localhost: 801/home.


What if we need to pass parameters to the view page in the code in the previous example? Modify the routing code:

Route: get ('/home', function () {return view ('Home', ['name' => 'Zhang San']);});
Modify View code:
[
 ], Hello!
Let's take a look at the access results:

If there are too many views, it is generally stored by module or even by function. in the resources/views Directory, create the directory public/demo in sequence, and then move home. php to the directory.

Modify the routing code in the preceding example:

Route: get ('/home', function () {return view ('public. demo. Home', ['name' => 'Zhang San']);});
You can still access the page again.

2.3 route parameters

As mentioned above, routes can match user request parameters. how can this problem be solved? Append the code segment to the routing file in the preceding example:

Route: get ('user/{name} ', function ($ name) {return 'user name:'. $ name ;});
Open the browser and access: http: // localhost: 801/user/jack.

What if there are two parameters? Modify the routing code:

Route: get ('user/{name}/{age} ', function ($ name, $ age) {return 'user name :'. $ name. ', age :'. $ age ;});
Open the browser and access: http: // localhost: 801/user/jack/23.


What if the age parameter is not required? Modify the routing code again:

Route: get ('user/{name}/{age ?} ', Function ($ name, $ age = null) {return 'user name:'. $ name. ', age:'. $ age ;});
Access address: http: // localhost: 801/user/jack

2.4 route parameter constraints

Under normal circumstances, some parameters accessed by users have certain rules. for example, the user ID when reading user information may be a number, and the news ID when modifying news information may be a GUID.

Modify the routes. php file and append the following code:

Route: get ('New/{id} ', function ($ id) {return 'news ID :'. $ id ;})-> where ('id', '[0-9] + ');
Open a browser to access http: // localhost: 801/new/3:


When http: // localhost: 801/new/abc is accessible, the provided page does not exist:


Correspondingly, when multiple parameters are limited at the same time, you need to use an array to modify the routing code of the preceding example:

Route: get ('New/{id}/{title} ', function ($ id, $ title) {return 'news ID :'. $ id. ', news Title :'. $ title ;})-> where (['id' => '[0-9] +', 'title' => '[a-z] +']);
The access results will not be demonstrated here.
In addition, we can configure global constraints to open the file: app/Providers/RouteServiceProviders. php. modify the boot method as follows:
    public function boot(Router $router)    {        //        $router->pattern('id', '[0-9]+');        parent::boot($router);    }

Modify the routing code in the preceding example:

Route: get ('New/{id} ', function ($ id) {return 'news ID:'. $ id ;});
The access effect is the same as that in the previous example.

2.5 obtain route parameters

In routes. php, you can obtain the route parameters to perform other operations. modify the routing code of the preceding example:

Route: get ('New/{id} ', function (Request $ request, $ id) {if ($ request-> route ('id ') = '2') {return 'news ID is 2';} else {return 'News ID is not 2 and the value is :'. $ id ;}});

In routes. php

use Illuminate\Http\Request;

Open your browser and access http: // localhost: 801/new/2 and http: // localhost: 801/new/3 to see different Page effects.


Routing also has many complex functions to be studied.

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 2: Laravel routing, including some content, and hope to be helpful to friends who are interested in PHP tutorials.

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.