Two tips for laravel PHP framework

Source: Internet
Author: User
Tags php framework

This article mainly introduced the PHP framework Laravel Two, this article explained Route::controller Middle Road by the batch naming, according to the system variable to judge the current operating environment two small skills, needs the friend to be possible to refer to under

Using Laravel as a PHP development framework has been a long time, but some of the official documents are not covered in the place, and will be forgotten at intervals. Recently done a bit of simple finishing, conveniently record the memo.

1. Route::controller Route naming:

Route::controller can reduce a lot of work in routing customization, but sometimes it is necessary to name a particular route to use, but the Route::controller method is to specify the route of all the methods in a controller, how do you name it? ? You can use the third argument in controller ($uri, $controller, $names = Array ()), which is an array whose key is the method, and the value of the array is named.

The code is as follows:

The signature of the function:

Public Function controller ($uri, $controller, $names = Array ())

Do not name general use:

Route::controller (' admin ', ' admincontroller ');

You need to name some of these methods:

Route::controller (' admin ', ' Admincontroller ', Array (

' GetIndex ' => ' Admin.index ',

' GetLogin ' => ' Admin.login ',

' Postlogin ' => ' Admin.login '

));

2. According to system variables to determine the current operating environment

The way the system defaults to determine whether the local environment is based on specifying a set of host names in the ' local ' array as the native environment, such as working on an office machine, a MacBook, and you're going to write two host names, I think it's a hassle. To be judged according to $_server[' Laravel_env ', so that I can define ' laravel_env ' environment variables in all the development machines, ' local ', and then automatically recognize the ' local ' environment in the development machine, while others are ' Production '.

The code is as follows:

The default is to determine whether the local environment is based on the host name

$env = $app->detectenvironment (Array (

' Local ' => array (' Homestead ');

));

Modify to determine whether the system variable is specified, not to determine the host name

$env = $app->detectenvironment (function () {

$_env = getenv (' laravel_env ')? getenv (' laravel_env '): Array (

' Local ' => array (' Homestead ')

);

return $_env;

});

This is actually going to read the value of $_server[' laravel_env '

In Apache, you can use the setenv setting,

In Nginx, you can use the Fastcgi_param setting

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.