LNMP环境配置laravel的注意事项

在用laravel开发接口的后,部署到centos(6.5)后。出现的一些问题记录:

1.上传根目录后,目录权限需要改为:www:www

2.nginx.conf 配置文件里面需要添加:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

3.涉及到文件上传到时候。需要手工的安装 php_file模块。这个地方注意 extension 等于 后面的后缀树 .so结尾的。而不是.dll

参考:

a. 跳转到fileinfo源代码目录` cd /root/downloads/lnmp1.2-full/src/php-5.6.9/ext/fileinfo/`
b. 执行 `/usr/local/php/bin/phpize`
c. 执行 `./configure –with-php-config=/usr/local/php/bin/php-config`
d. 执行 `make && make install`
e. 修改php.ini文件 `vi /usr/local/php/etc/php.ini` 在末尾增加`extension = fileinfo.so`即可
f. 重启php-fpm ,`/etc/init.d/php-fpm reload`

laravel自定义验证

// 输入验证
       $rules = [
           ’email’ => ‘required|unique:posts|max:255’,
           ‘name’ => ‘required|between:1,20’,
           ‘body’ => ‘required’,
       ];
       $message = [
           ‘required’ => “:attribute 不能为空”,
           “between” => “:attribute 长度必须在 :min 和 :max 之间”
       ];
       $attributes = array(
           “email” => ‘电子邮件’,
           ‘name’ => ‘用户名’,
           ‘body’ => ‘内容’,
       );

       $validator = Validator::make($request->all(), $rules, $message, $attributes);

       if ($validator->fails()) {

           return response()->json([‘code’ => ‘-1’, ‘message’ => $validator->errors()->all()]);
       }

中间件传值给控制器

// web.php
Route::get('/check', '[email protected]')->middleware(App\Http\Middleware\CheckRequest::class);

// Middleware/CheckRequest.php
class CheckRequest
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $check_request = 'CheckRequest';
        $request->attributes->add(compact('check_request'));
        return $next($request);
    }
}

// CheckController.php
//use Request;
use Illuminate\Http\Request;
class CheckController extends Controller
{
    public function check(Request $request)
    {
        return $request->get('check_request'); // 输出CheckRequest
    }
    
    public function check2()
    {
        return Request::get('check_request'); // 输出CheckRequest
    }
}

laravel 传递变量方式

 

1.使用with()方法

public function index() { $title = '文章标题1'; return view('articles.lists')->with('title',$title); }

 

 

<body>
<h1><?php echo $title; ?></h1>

</body>

 

blade模板中:

<body>
<h1>{{ $title }}</h1>

</body>

{{ $title }} ,有可能你的变量含有html标签。但会原样输出

如果你想将 $title 作为页面元素渲染输出,你需要这样写:

<h1>{!! $title !!}</h1>

 

2.直接给view()传参数

public function index() { $title = '<span style="color: red">文章</span>标题1'; return view('articles.lists',['title'=>$title]); }

多个:

public function index()
   {
       $title = '<span style="color: red">文章</span>标题1';
       $intro = '文章一的简介';
       return view('articles.lists',[
                                       'title'=>$title,
                                       'introduction'=>$intro
                                       ]);
   }

 

输出

<body>
<h1>{!! $title !!}</h1>
<p>{{ $introduction }}</p>
</body>

 

3.使用compact
 public function index()
    {
        $title = '<span style="color: red">文章</span>标题1';
        $intro = '文章一的简介';
        return view('articles.lists',compact('title','intro'));
    

 

@yield(‘content’) 模板占位符

继承的子类用

@section(‘content’)
<h1>{!! $title !!}</h1>
<p>{{ $intro }}</p>
@endsection

实现改变,客制化。