Laravel 5 Basic Tutorials | | 2. Routing: Link to Controller's connector

Source: Internet
Author: User
Laravel 5 Basic Tutorials | | 2. Routing: Link to Controller-table seriousness

Intuitively, routing is used to parse the part of the address bar after the domain name.

Example: Baidu.com/lala

Note the/lala after the domain name, this is a routing rule. Routing rules allow us to flexibly control the scope and depth of user access, and can even be used for rights management in Laravel.

Create a route

Routing files

For example, if your Laravel project directory is called MySite, then the routing control file is in/mysite/app/http/routes.php

This file has only one rule at the beginning, like this (if you find more than one, it doesn't matter, anyway, no matter how much we will delete the rewritten, wayward (? ^?)):

This is a definition of the first page of the routing rules, that is, this rule allows us to see the default home page Laravel, that is, after the successful installation of the interface. (similar to below)

Route::get ('/', function () {    return view (' Welcome ');});

Shameless hard wide into

Laravel 5 Video Tutorial Address-table serious lecture (can audition)
All courses: biaoyansu.com

Shameless hard wide out

First Route ~

Route::(' hello ', function () {    return ' hello. ';});

Explanation: The first pass is the address of the route (example: +baidu.com/hello), the second argument is a function, and the smart one must be able to guess (mua~) This function is triggered when the route matches, if your current test address is +localhost:8888 then, Then when you enter +localhost:8888/hello in the browser address bar and then come to a low-key return you will see a low-key "Hello."

The attentive classmate may find that we are using return instead of ECHO, why can we print the string directly? Laravel is such a small vest, when he detects that you return directly to the string will use echo, more than that, when the returned array is detected Laravel will convert the array to JSON return (warm male one, identification completed).

Route::get (' Hello ', function () {    return [        ' greet ' = ' hello ',        ' name '  = ' xiaobeixin '    ];});

Will output:

Route-Pass parameters

What if we want to add a person's name dynamically after Hello?
Do this:

Route::get (' hello/{name} ', function ($name) {    return ' hello '. $name. '.'; });

Address bar Pass parameter localhost:8888/hello/liming

Output:

So how to output an HTML file ray?

Don't worry, talk with the view back.

  • 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.