Laravel Framework Routing configuration summary, Setup tips Daquan, laravel framework _php Tutorial

Source: Internet
Author: User

Laravel Framework Routing configuration summary, Setup tips Daquan, Laravel framework


Basic routing

Most of the routes for your application will be defined in the app/routes.php file. The simplest route in Laravel consists of a URI and a closure call.

Basic GET Route
Copy the Code code as follows:
Route::get ('/', function ()
{
Return ' Hello world ';
});

Basic POST Routing
Copy the Code code as follows:
Route::p ost (' Foo/bar ', function ()
{
Return ' Hello world ';
});

Register a route in response to all HTTP methods
Copy the Code code as follows:
Route::any (' foo ', function ()
{
Return ' Hello world ';
});

Forcing a route to be accessed over HTTPS
Copy the Code code as follows:
Route::get (' foo ', Array (' HTTPS ', function ()
{
Return ' must is over HTTPS ';
}));

Often you need to generate URLs based on routes that you can use by using the Url::to method:
Copy the code as follows: $url = url::to (' foo ');

Route parameters
Copy the Code code as follows:
Route::get (' User/{id} ', function ($id)
{
Return ' User '. $id;
});

Optional routing parameters
Copy the Code code as follows:
Route::get (' user/{name} ', function ($name = null)
{
return $name;
});

Optional route parameters with default values
Copy the Code code as follows:
Route::get (' user/{name} ', function ($name = ' John ')
{
return $name;
});

Routing with regular expression constraints
Copy the Code code as follows:
Route::get (' user/{name} ', function ($name)
{
//
})
->where (' name ', ' [a-za-z]+ ');
Route::get (' User/{id} ', function ($id)
{
//
})
->where (' id ', ' [0-9]+ ');

Routing Filters

Route filters provide a simple way to restrict access to a specified route, which is useful when you need to create an authentication zone for your site. The Laravel framework contains a number of routing filters, such as auth filters, Auth.basic filters, guest filters, and CSRF filters. They are stored in the app/filters.php file.

Define a route filter
Copy the Code code as follows:
Route::filter (' old ', function ()
{
if (Input::get (' age ') < 200)
{
Return redirect::to (' home ');
}
});

If a response is returned from a route filter, the response is considered a response to the request, the route will not be executed, and any after filter on the route will be canceled.

Specify a route filter for a route
Copy the Code code as follows:
Route::get (' user ', Array (' before ' = ' old ', function ()
{
Return ' You were over years old! ';
}));

Specify multiple route filters for a route

Copy the Code code as follows:
Route::get (' user ', Array (' before ' = ' auth|old ', function ()
{
Return ' You were authenticated and over years old! ';
}));

Specifying route filter Parameters
Copy the Code code as follows:
Route::filter (' Age ', function ($route, $request, $value)
{
//
});
Route::get (' user ', Array (' before ' = ' age:200 ', function ()
{
Return ' Hello world ';
}));

When the route filter receives the response as a third parameter $response:
Copy the Code code as follows:
Route::filter (' Log ', function ($route, $request, $response, $value)
{
//
});

Patterns for basic route filters

You may want to specify a filter based on a URI for a set of routes.
Copy the Code code as follows:
Route::filter (' admin ', function ()
{
//
});
Route::when (' admin/* ', ' admin ');

In the example above, the admin filter will be applied with all routes beginning with admin/. The asterisk, as a wildcard, is adapted to the combination of all characters.

You can also constrain a pattern filter by specifying an HTTP method:

Copy the Code code as follows:
Route::when (' admin/* ', ' admin ', array (' post '));

Filter class

For advanced filters, you can use a class instead of a closure function. Because the filter class is an IoC container that is outside the application, you can use dependency injection in the filter to make it easier to test.

Define a filter class
Copy the Code code as follows:
Class Foofilter {
Public Function Filter ()
{
Filter logic ...
}
}

Registering a class-based filter
Copy the Code code as follows:
Route::filter (' foo ', ' Foofilter ');

Named routes

Named routes make it easier to specify routes when generating jumps or URLs. You can specify a name for the route like this:
Copy the Code code as follows:
Route::get (' User/profile ', Array (' as ' = ' profile ', function ()
{
//
}));

You can also specify the route name for the controller's methods:
Copy the Code code as follows:
Route::get (' User/profile ', Array (' as ' = ' profile ', ' uses ' =
' Usercontroller@showprofile '));

Now you use the name of the route when generating URLs or jumps:

Copy the Code code as follows:
$url = Url::route (' profile ');
$redirect = Redirect::route (' profile ');

You can use the Currentroutename method to get the name of a route:

Copy the Code code as follows:
$name = Route::currentroutename ();

Routing groups

There are times when you might want to apply filters to a set of routes. You do not need to specify a filter for each route, you can use routing groups:
Copy the Code code as follows:
Route::group (Array (' before ' = ' auth '), function ()
{
Route::get ('/', function ()
{
Has Auth Filter
});
Route::get (' User/profile ', function ()
{
Has Auth Filter
});
});

Sub-domain Routing

The Laravel route can also handle wildcard subdomains and get wildcard parameters from the domain name:

Registering sub-domain Routing
Copy the Code code as follows:
Route::group (Array (' domain ' = ' = ' {account}.myapp.com '), function ()
{
Route::get (' User/{id} ', function ($account, $id)
{
//
});
});

Route prefixes

A set of routes can be prefixed to a routing group by using the prefix option in an attribute array:

To add a prefix to a routing group
Copy the Code code as follows:
Route::group (Array (' prefix ' = ' admin '), function ()
{
Route::get (' User ', function ()
{
//
});
});

Routing model bindings

Model binding provides a simple way to inject a model into a route. For example, not only is the ID of a user injected, you can inject the entire user model instance according to the specified ID. First use the Route::model method to specify the desired model:

Binding a variable to a model
Copy the Code code as follows:
Route::model (' user ', ' user ');

Then, define a route that contains the {user} parameter:
Copy the Code code as follows:
Route::get (' Profile/{user} ', function (user $user)
{
//
});

Because we have bound the {user} parameter to the user model, a user instance is injected into the route. So, for example, a PROFILE/1 request will inject a User instance with ID 1.

Note: If the model instance is not found in the database, a 404 error will be thrown.

If you want to specify a behavior that you define that you do not find, you can pass a closure as the third parameter for the model method:
Copy the Code code as follows:
Route::model (' user ', ' user ', function ()
{
throw new Notfoundexception;
});

Sometimes you want to use your own method to handle routing parameters, you can use the Route::bind method:
Copy the Code code as follows:
Route::bind (' User ', function ($value, $route)
{
Return User::where (' name ', $value)->first ();
});

404 Error raised

There are two ways to manually trigger a 404 error in a route. First, you can use the App::abort method:

Copy the Code code as follows:
App::abort (404);

Second, you can throw an instance of symfony\component\httpkernel\exception\notfoundhttpexception.

More information about handling 404 exceptions and using custom responses for these errors can be found in the error section.

Routing to a controller

Laravel not only allows you to route to closures, it can also be routed to the Controller class, and even allows the creation of resource controllers.

For more information, please visit the controller documentation.


No line by how to configure the connection can be entered into the company intranet

You can't set the IP settings for 2 computers. Try 192.168.133.32 or other numbers, but not 33.

No line by how to set up to the company network

First of all, you should be to a fixed IP address, your company's network itself is the LAN intranet IP, you can also use any of your computer IP as a non-line used by the static IP, for example: 192.168.2.101, and then you set the following method

Wireless Router Setup Tutorial Example
Before configuring the wireless router, we first need to connect the relevant lines. First plug the Internet-connected network cable into the WAN port of the wireless router, and then we need a computer to configure the router with a LAN port that connects to the router via a network cable. The first step is to ensure that the local computer operating system has the TCP/IP protocol installed, which can be ignored for users of the Windows 2000 level. Because the router default address is 192.168.1.1, the subnet mask is 255.255.255.0, so we must manually set the local connection address to the same network segment in order to properly configure the router, that is, the address of the local connection is set to 192.168.1.xxx (xxx=2~254).

The subnet mask is 255.255.255.0. Open IE input after Setup is complete 192.168.1.1 The default address for no line will pop up the above window, asking the user to enter the administrator's user name and password. The user name and password can be obtained from the product specification, which is generally admin.

General routers can directly through the Web directly managed, and this router is the same, the interface using the whole Chinese setting, for domestic users will bring some convenience. After landing, ie automatically pops up a window for the Product Setup Wizard to make it easy and quick for the user to complete the wireless router setup. Click Next to provide 3 of the most common network landing mode, with the most common ADSL, for example, we choose PPPoE Virtual Dial-up method Click Next, then asked to enter the login network account and password, and then click Next to enter the wireless Settings page.

Here is a brief introduction to the wireless Router Setup tutorial This page of the detailed features of several options, wireless function If selected to open, the wireless network access to the host will be able to access the limited network, the SSID number, which is the wireless LAN for authentication of the login name, only authenticated users can access the wireless network;

Frequency bands, which are used to determine the wireless frequency segment used by the wireless router, are selected from 1~11, where most of the 11 channels are used, and exactly two signals in the 2.4GHz segment, only 4 or more of the frequencies, the signal does not interfere with each other (so usually using 6 channels and 11 channels). mode, you can choose 802.11B mode with 11Mbps bandwidth, 802.11g mode with 54Mbps bandwidth (also compatible with 802.11b mode). After the configuration is complete, click Next to finish.

When we are done, we click on the Web page and we can see that the router is still not making a dial-up connection, and we need to manually tap the connection to access the Internet. After the connection, we can see in this page the various states in the router, such as the LAN port status is the default address of the current router, the wireless status is no line by the relevant setting options and the WAN port status is access to the Internet after the address and the ISP vendor's gateway and DNS server address, You can also count the online time of the router and the connection status of the control router.

Completed the above step, no line by the configuration has been basically completed, the local network through the cable connection of the computer and the computer through the wireless network card can be connected through the Internet to achieve the function of the network, so that the configuration of no line by and not as everyone thought so abstract and complex.

The configuration of the wireless card terminal is also very simple, after inserting the wireless network card according to the prompts after installing the relevant driver can be used. When the user finishes configuring the wireless router, the computer that installs the wireless card automatically searches for the relevant wireless network, and then the user clicks the connection to easily connect to the wireless LAN for file sharing and Internet connectivity. When the user clicks the icon of the wireless network status in the lower right corner, the above window will appear, which can show the network connection speed and the intensity of the signal, although the signal strength is not very accurate, but there is a certain reference value. In this window ... Remaining full text >>

http://www.bkjia.com/PHPjc/874109.html www.bkjia.com true http://www.bkjia.com/PHPjc/874109.html techarticle Laravel Framework Routing configuration summary, Setup tips Daquan, laravel framework Basic routing your application's vast majority of routes will be defined in the app/routes.php file. The simplest of the Laravel ...

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