PHP Framework Laravel Learning Three (basic function)

Source: Internet
Author: User
Tags php framework
One, routing

Routing is actually the instruction used in the browser to send HTTP requests, and many API functions are actually sending requests via URLs.

HTTP request mode and response is a basic course, but a lot of content, should first study well.

Laravel routing differs from other frameworks and needs to be customized individually, with the benefits of being powerful, flexible, and easy to manage.

3 Basic Ways

First Kind
is Route::get (' path ', function () {...}); In this form, you can change the get to post, put, delete and other request methods according to the need, which is the most common and recommended;

A route can be followed by a closure (anonymous function) to execute a piece of code directly, this is not a lot of frameworks, in fact, it is not likely to write business code in this small closure, but put into the controller, so the most common way is route::get (' path ', Xxxcontroller@action);

The most useful use of a routed closure function is to test a function or code, which is handy for writing, and I usually keep a route::get (' Test ', function () {...}); Do the testing.

There are often many routes in the project, one to write some people feel slow. Some crud are very standardized, so so-called restful routing (Controller).

The second type:
Route::resource (' posts ', ' Postscontroller ');

To write a route like this, in fact, generate 7 routes by the name of the route, the path form of the 7 routes is based on the restful standard, so there is also called rest routing, but this route has a limitation, in the corresponding controller, in addition to rest corresponding to the 7 methods, other methods if you want to access, To define a route in front of a restful route to access it.

The third type:
Called a control route, also known as an implicit route:
Route::controller (' foo ', ' Foocontroller ');

This is a very flexible way to use the Magic method to set the method name HTTP request mode, which is very convenient for some API-style requests.

The name of the implicit route needs to be noted, because it is not a write-by-route, as follows:

Route::controller('users''UserController', [    'anyLogin''user.login',]);

The preceding is the method name, followed by the route name;

Route Name
Anyway, what's the use of naming a route? Useful, because the route (URL) is used in a large number of view files, with the name of the route can be decoupled, you can modify the URL arbitrarily, without affecting the URL in the view;

Route parameters
We usually say that the routing parameters generally look like this index.php?user=ooo&psw=xxx; But in Laravel's view, this clear way is ugly and unsafe. So it's written like this:

Route::get('user/{name}'function($name){//可以直接使用$name})

This parameter value, if it is a string, cannot be the same as other route names, and the system takes precedence over the route so that no value is taken.
About the value of this block, the first is actually can be directly transmitted value, in addition, you can also use the object method totake the value, the official website shows the comparison pit, rout e? > I N P u T (id ) This one

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

The above describes the PHP framework Laravel Learning Three (basic functions), 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.