Laravel in-depth understanding of Routing and URL generation

Source: Internet
Author: User
Laravel by URL generation and routing

In the template we generally do not directly write dead URLs, but instead use the URL assistant to generate the URL, this article describes the use of the URL Assistant and some of the more troublesome problems encountered.

First, we create a route:

Route::get (' articles ', [' uses ' = ' articlescontroller@index ', ' as ' = ' articles.index ']);

Assuming our project is deployed at the root of the domain name, it can be accessed via the following URL:

Http://localhost/articles

Now, we generate links in the template in several ways:

Simple mode

Link//or link//for easy reading, omit HTML tag below

In this way, simply stitch the path you specify to the site root URL.

Route mode

Url::route (' Articles.index ')

This approach is to specify a URI that matches the ' as ' parameter when registering the route and gets the registration.

Controller action Mode

Url::action (' Articlescontroller@index ')

This approach is based on the ' uses ' parameter of the registration route, which automatically generates the URI mapped to the controller method, with the rule route::controller ().
Examples are as follows:

Articlescontroller@index = Articlesarticlescontroller@getadd = Articles/addarticlescontroller@postadd = Articles/addarticlescontroller@getdelete = Articles/delete

The basic tutorial ends here, and then we face some annoying situations.

Now that the routing becomes more complex, we define one such:

Route::controller (' Users ', ' Userscontroller ');

A simple statement, Laravel will automatically reflect the Userscontroller class, and scan the method, and then generate a normal mapping, for example, assume that the controller has the following methods:

function getindex (); function getedit (); function Postedit ();

In fact, through Route::controller () The final result * * similar to:

Route::get (' users ', [' uses ' = ' userscontroller@getindex ']); Route::get (' Users/edit ', [' uses ' = ' userscontroller@getedit ']); Route::p ost (' Users/edit ', [' uses ' = ' userscontroller@postedit ']);

To be blunt, advanced routing methods are just encapsulation of basic methods.

OK, now let's build a URL:

echo url::action (' Userscontroller@getedit ');

Get Http://localhost/users/edit, but we need to add querystring parameters? Also known as the Get parameter.

What do we want to get http://localhost/users/edit?id=1 to generate?

The wise man has noticed. Url::action () has a second argument, you can specify an array as the URL parameter, well, let's try.

echo url::action (' Userscontroller@getedit ', [' ID ' =>1]);

All right? no!! What you get will be:

Http://localhost/users/edit/1

If you add one more parameter:

echo url::action (' Userscontroller@getedit ', [' id ' =>1, ' author ' = ' admin ']);

What you get is:

Http://localhost/users/edit/1/admin

It ignores the key you specified and does not have a id=1&author=admin appended to the end of the URL.

Why?!!

To know the answer, read on: Laravel deep understanding of Routing and URL generation

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