Home  >  Article  >  Backend Development  >  Laravel framework routing configuration summary and setting tips, laravel framework_PHP tutorial

Laravel framework routing configuration summary and setting tips, laravel framework_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:19:31843browse

Laravel framework routing configuration summary and setting tips, laravel framework

Basic routing

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

Basic GET routing

Copy code The code is as follows:

Route::get('/', function()
{
return 'Hello World';
});

Basic POST routing
Copy code The code is as follows:

Route::post('foo/bar', function()
{
return 'Hello World';
});

Register a route to respond to all HTTP methods
Copy code The code is as follows:

Route::any('foo', function()
{
Return 'Hello World';
});

Force a route to be accessed via HTTPS
Copy code The code is as follows:

Route::get('foo', array('https', function()
{
Return 'Must be over HTTPS';
}));

Often you need to generate URLs based on routes, you can do this by using the URL::to method:
Copy code The code is as follows:
$url = URL::to('foo');

Routing parameters

Copy code The code is as follows:

Route::get('user/{id}', function($id)
{
return 'User '.$id;
});

Optional routing parameters
Copy code The code is as follows:

Route::get('user/{name?}', function($name = null)
{
return $name;
});

Optional routing parameters with default values
Copy code The code is as follows:

Route::get('user/{name?}', function($name = 'John')
{
return $name;
});

Routing with regular expression constraints
Copy code The code is as follows:

Route::get('user/{name}', function($name)
{
//
})
->where('name', '[A-Za-z]+');
Route::get('user/{id}', function($id)
{
//
})
->where('id', '[0-9]+');

Route Filter

Route filters provide a simple way to restrict access to specific routes, which is useful when you need to create zones for your site that require authentication. The Laravel framework contains some routing filters, such as auth filter, auth.basic filter, guest filter, and csrf filter. They are stored in the app/filters.php file.

Define a route filter

Copy code The code is 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 filters on the route will be canceled.

Specify a route filter for a route

Copy code The code is as follows:

Route::get('user', array('before' => 'old', function()
{
return 'You are over 200 years old!';
}));

Specify multiple route filters for a route

Copy code The code is as follows:

Route::get('user', array('before' => 'auth|old', function()
{
return 'You are authenticated and over 200 years old!';
}));

Specify route filter parameters
Copy code The code is 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 the third parameter $response:
Copy code The code is as follows:

Route::filter('log', function($route, $request, $response, $value)
{
//
});

Basic route filter mode

You may wish to specify filters for a set of routes based on URI.

Copy code The code is as follows:

Route::filter('admin', function()
{
//
});
Route::when('admin/*', 'admin');

In the example above, the admin filter will be applied to all routes starting with admin/. The asterisk acts as a wildcard character and will match all character combinations.

You can also constrain pattern filters by specifying the HTTP method:

Copy code The code is 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 lives outside the application, you can use dependency injection in the filter, making it easier to test.

Define a filter class

Copy code The code is as follows:

class FooFilter {
public function filter()
{
// Filter logic...
}
}

Register a class-based filter
Copy code The code is as follows:

Route::filter('foo', 'FooFilter');

Named Route

Named routes make it easier to specify routes when generating redirects or URLs. You can give the route a name like this:

Copy code The code is as follows:

Route::get('user/profile', array('as' => 'profile', function()
{
//
}));

You can also specify route names for controller methods:
Copy code The code is as follows:

Route::get('user/profile', array('as' => 'profile', 'uses' =>
'UserController@showProfile'));

Now you use the route name when generating URLs or redirects:

Copy code The code is as follows:

$url = URL::route('profile');
$redirect = Redirect::route('profile');

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

Copy code The code is as follows:

$name = Route::currentRouteName();

Routing Group

Sometimes you may want to apply a filter to a set of routes. You don't have to specify filters for each route, you can use route groups:

Copy code The code is as follows:

Route::group(array('before' => 'auth'), function()
{
Route::get('/', function()
{
// Has Auth Filter
});
Route::get('user/profile', function()
{
// Has Auth Filter
});
});

Subdomain name routing

Laravel routing can also handle wildcard subdomains and obtain wildcard parameters from the domain name:

Register subdomain routing

Copy code The code is as follows:

Route::group(array('domain' => '{account}.myapp.com'), function()
{
Route::get('user/{id}', function($account, $id)
{
//
});
});

Routing prefix

A group of routes can be prefixed by using the prefix option in the properties array:

Add prefix to routing group

Copy code The code is as follows:

Route::group(array('prefix' => 'admin'), function()
{
Route::get('user', function()
{
//
});
});

Route model binding

Model binding provides a simple way to inject models into routes. For example, instead of just injecting a user's ID, you can inject an entire user model instance based on a specified ID. First use the Route::model method to specify the required model:

Bind a variable to the model

Copy code The code is as follows:

Route::model('user', 'User');

Then, define a route containing the {user} parameter:
Copy code The code is as follows:

Route::get('profile/{user}', function(User $user)
{
//
});

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

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

If you wish to specify your own behavior that is not found, you can pass a closure as the third parameter to the model method:

Copy code The code is as follows:

Route::model('user', 'User', function()
{
throw new NotFoundException;
});

Sometimes you want to use your own method to handle route parameters, you can use the Route::bind method:
Copy code The code is as follows:

Route::bind('user', function($value, $route)
{
return User::where('name', $value)->first();
});

Raises 404 error

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

Copy code The code is as follows:

App::abort(404);

Second, you can throw an instance of SymfonyComponentHttpKernelExceptionNotFoundHttpException.

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

Route to controller

Laravel not only allows you to route to closures, but also to controller classes, and even allows you to create resource controllers.

Please visit the controller documentation for more information.

How to configure the wireless router to access the company intranet

You cannot set the IP of two computers to be the same. Try 192.168.133.32 or other numbers but not 33

How to set up wireless routing so that you can connect to the company network

First of all, you should get a fixed IP address. Your company's network itself is the LAN IP. You can also use any computer IP you use as a static IP for wireless routing, for example: 192.168. 2.101, then you set it up as follows

Wireless Router Setting Tutorial Example
Before configuring the wireless router, we must first connect the relevant lines. First plug the network cable connected to the Internet into the WAN port of the wireless router. Then we need a computer to connect to the LAN port of the router through the network cable to perform relevant configurations on the router. First, make sure that the local computer operating system has the TCP/IP protocol installed. This step can be ignored for users with Windows 2000 or above. Since the default address of the router is 192.168.1.1 and the subnet mask is 255.255.255.0, we must manually set the local connection address to be within the same network segment to configure the router normally, that is, set the local connection address to 192.168. 1.xxx (xxx=2~254).

The subnet mask is 255.255.255.0. After the setting is completed, open IE and enter the default address of 192.168.1.1 wireless router. The above window will pop up, asking the user to enter the administrator's user name and password. The username and password can be obtained from the product manual, usually admin.

General routers can be managed directly through the Web, and the same is true for this router. The interface uses full Chinese settings, which will bring certain convenience to domestic users. After logging in, IE automatically pops up a window with a product setup wizard that allows users to quickly and easily complete the settings of the wireless router. After clicking Next, the three most common network login methods are provided. Taking the most common ADSL as an example, we choose the PPPoE virtual dial-up method and click Next. Then we are asked to enter the account and password to log in to the network, and then click Next to enter. Wireless settings page.

Here is a brief introduction to the detailed functions of several options on the wireless router setup tutorial page. If the wireless function is selected to be turned on, the host connected to the wireless network will be able to access the limited network; the SSID number, that is, The login name of the wireless LAN is used for authentication. Only users who pass the authentication can access the wireless network;

frequency band is used to determine the wireless frequency band used by the wireless router. The selection range is from 1 to 11. The most commonly used channel is channel 11, which is two signals in the 2.4GHz band. Only if there is a frequency difference of more than 4, the signals will not interfere with each other (so channel 6 and channel 11 are usually used together). Mode, you can choose the 802.11b mode with 11Mbps bandwidth and the 802.11g mode with 54Mbps bandwidth (also compatible with 802.11b mode). After the configuration is completed, click Next to complete.

After completion, we click on the running status on the Web page. We can see that the router still does not have a dial-up connection. We need to manually click on the connection before we can access the Internet. After connection, we can see various statuses in the router on this page, such as LAN port status, which is the default address of the current router, wireless status, which is the relevant setting options in wireless routing, and WAN port status, which is obtained after accessing the Internet. address as well as the gateway and DNS server address of the ISP provider, and can also count the online time of the router and control the connection status of the router.

After completing the above steps, the configuration of the wireless router can be said to be basically completed. Computers connected locally through network cables and computers connected through wireless network cards can realize the Internet access function through wireless routing, so the configuration Wireless routing is not as abstract and complex as everyone thinks.

The setting of the wireless network card is also very simple. After inserting the wireless network card, install the relevant driver according to the prompts and you can use it. After the user completes the configuration of the wireless router, the computer with the wireless network card installed will automatically search for the relevant wireless network, and then the user can easily connect to the wireless LAN by clicking Connect to realize file sharing and Internet connection functions. When the user clicks the wireless network status icon in the lower right corner, the above window will appear. This window can display the network connection speed and signal strength. Although the signal strength is not very accurate, it still has certain reference value. In this window...the rest of the text>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/874109.htmlTechArticleLaravel framework routing configuration summary and setting tips, laravel framework basic routing, most of the routing of your application will Defined in the app/routes.php file. The simplest in Laravel...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn