laravel技巧-多语言提示、公用函数引入、url忽略大小写

创建一个laravel5.1项目easy_grouping。
 composer create-project laravel/laravel easy_grouping 5.1.*

1.多语言提示

在resources-lang目录下新建目录zh-cn,创建tip.php,内容如下
<?php
return ['200'=>'操作成功'];
修改config/app.php
'locale' => 'zh-cn',
在接口中
echo trans('tip.200');
输出“操作成功”

2.引入公用函数

在app目录下创建Helper目录,创建common_functions.php,内容如下
<?php
function test()
{
echo 'common funciton include succeed';
}
在composer.json中增加
"autoload": {
    "files": [
      "app/Helper/common_funtions.php"
    ]
  }
命令行中执行
composer dump-autoload
test()函数就可全局使用了。

3.url忽略大小写

在上面创建的Helper文件夹下,创建CaseInsensitiveUriValidator.php,内容如下
<?php namespace App\Helper;

use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Routing\Matching\ValidatorInterface;

class CaseInsensitiveUriValidator implements ValidatorInterface
{
    public function matches(Route $route, Request $request)
    {
        $path = $request->path() == '/' ? '/' : '/'.$request ->path();
        return preg_match(preg_replace('/$/','i', $route->getCompiled()->getRegex()), rawurldecode($path));
    }
}
在routes.php中引入
use Illuminate\Routing\Route as IlluminateRoute;
use App\Helper\CaseInsensitiveUriValidator;
use Illuminate\Routing\Matching\UriValidator;

$validators = IlluminateRoute::getValidators();
$validators[] = new CaseInsensitiveUriValidator;
IlluminateRoute::$validators = array_filter($validators, function($validator) {
    return get_class($validator) != UriValidator::class;
});














猜你喜欢

转载自blog.csdn.net/qq_34881718/article/details/77726611