laravel笔记2后台配置

Laravel5.8入门与实战-安全与优化

表单验证提示信息

     if ($result===true || $result>0) {
            $request->session()->flash('data',['class'=>'success','msg'=>'更新成功']);
        }else{
            $request->session()->flash('data',['class'=>'danger','msg'=>'更新失败']);
        }
        return redirect(route('admin.config.siteconfig'));
    }

表单验证blade里面

         @if (session()->has('data'))
                  <div class="alert alert-{{session('data')['class']}}">
                    {{session('data')['msg']}}
                  </div>
              @endif
 

数据验证

private function checkdata($name='siteconfig',$request){
        switch($name){
            case 'infomation':
                $datavalidate = $request->validate([
                    'company'=>'required',
                    'address'=>'required',
                    'phone'=>'required',
                ],[
                    'company.required'=>'公司名称不能为空',
                    'address.required'=>'地址不能为空',
                    'phone.required'=>'客服热线不能为空',

                ]);   
                break;
            case 'baidu':
                $datavalidate = $request->validate([
                    'key'=>'required',
                ],[
                    'key.required'=>'百度密钥不能为空',
                ]);   
                break;
            default:
                $datavalidate = $request->validate([
                    'name'=>'required',
                    'tittle'=>'required',
                    'sitetittle'=>'required',
                    'domain'=>'required|url',
                ],[
                    'name.required'=>'配置标识不能为空',
                    'tittle.required'=>'配置名称不能为空',
                    'sitetittle.required'=>'网站名称不能为空',
                    'domain.required'=>'网站域名不能为空',
                    'domain.url'=>'网址输入不正确',
                ]);   
                break;

 

//根据配置标识获取配置信息

    private function getconfig($name = 'siteconfig'){
        $res_config = DB::table('config')->where('name','=',$name)->first();
        $config = [];
        if ($res_config){
            # code...
            $config = json_decode($res_config->config,true);
        }
        return $config;
    }
        }

后台设置信息设置完成后跳转到当前设置页面

   return redirect(route('admin.config.'.$request->name));
 

使用pjax实现网站后台页面部分刷新

命令composer require spatie/laravel-pjax

 kernel.php添加中间件

protected $middleware = [
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        \App\Http\Middleware\TrustProxies::class,
        \Spatie\Pjax\Middleware\FilterIfPjax::class
posted @ 2020-08-04 18:24  孤单照夜清  阅读(145)  评论(0编辑  收藏  举报