首页 > 技术文章 > laravel学习笔记(三)

wanghaokun 2018-11-22 09:00 原文

  • 模型传值

路由:

Route::get('/posts/{post}','\App\Http\Controllers\PostController@show');

方法:

public function show(Post $post){
    $isShowEdit = false;
    return view("post/show",compact,'isShowEdit','post'));
}
  • CSRF保护

laravel之伪造跨站请求保护CSRF实现机制:https://www.cnblogs.com/wanghaokun/p/10011668.html

Laravel 下的伪造跨站请求保护

{{csrf_field()}}
<input type='hidden' name='_token' value='{{csrf_token}}' />

X-CSRF-TOKEN

<meta name="csrf-token" content="{{ csrf_token() }}">


$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
  • 调试方法

dd:dump and die

dd(\Request::all())
dd(request());
dd(request('key'));
  • 数据库字段保护

protected $guarded=[];                         //不可以注入的数据字段
protected $fillable=['title','content'];      //可以注入的数据字段
  • 保存model


//        $post = new Post();
// $post->title = request('title');
// $post->content = request('content');
// $b = $post->save(); //true

//
$params = ['title'=>request('title'),'content'=>request('content')]; // Post::create($params); Post::create(request(['title','content'])); // dd(request()->all()); return(redirect('/posts'));
  •  验证错误提示

参考地址:Http层-表单验证  https://laravel-china.org/docs/laravel/5.4/validation/1234

eg:

$this->validate(request(),[
    'title'     => 'required|string|max:100|min:5',
    'content'   => 'required|string|min:10'
]);

验证提示汉化

resources\lang 下en 的文件夹 复制在同一目录并改名为 zn

把zn 中的 validation.php文件修改为对应汉化,https://laravel-china.org/articles/5840/validation-validation-in-laravel-returns-chinese-prompt

修改config 目录下的app.php 文件,locale' => 'en' 改为 locale' => 'en'

  • 图片上传

修改图片上传软链目录 config/filesystems.php 'default' => env('FILESYSTEM_DRIVER', 'public')

storage:link Create a symbolic link from "public/storage" to "storage/app/public"

php artisan storage:link

 

推荐阅读