laravel开发博客遇到的问题(一)
1.引入css,js,img等最好使用asset
使用此方式,获取的是带域名的绝对路径
2.测试数据库是否连接正常数据库
use DB;
$pdo = DB::connection()->getPdo();
dd($pdo);
3.引入第三方类
laravel,没有现成的验证码函数,需要引入验证码类
引入:
require_once('resource/code/Code.class.php');
使用:
$code = new \Code;
输出到页面(加上随机数,避免当做伪加载):
...
4.开启session
引入的第三方类库中普遍使用$_SESSION,
laravel没有开启原始的SESSION,因此需要在入口文件中打开
session_start();
5.表单csrf验证
laravel中对表单进行了安全设置,需要添加表单中加入
{{csrf_field()}}
6.判断是否是post传参
if($input = Input::all()){
//post传参,进行数据操作
}else{
//非post传参,加载页面
}
7.view中的if
@if(session('msg'))
.........
@endif
8.返回上级back/重定向redirect
if ($input['code'] != $_code) {
reurn back()->with('msg','验证码错误');
}
return redirect('admin/login');
9.crypt加密
crypt加密长度不固定 最大不超255
数据库类型: varchar 255
Crypt::encrypt($str);//加密
Crypt::decrypt($str);//解密
10.model
命令行创建:php artisan make:model User
在Http中创建Model文件夹,并修改如下,以下文件在app下
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $table='user';
protected $primaryKey='is';
public $timestamps=false;
...
}
//controller使用
$user = User::all();//获取所有
$user = User::first();//获取第一条,用于用户登录
if($user->name != $input['name']){
return back()->with('msg','账号错误');
}
11.session
存入
session(['user'=>$user]);
清除
session(['user'=>null]);
12.页面的继承
非公共部分
@yield('content')
继承:
@extends('Index.index')
@section('content')
.....
@endsection
13.中间层
Tttp/Kernel.php中添加
'admin.login' => \Illuminate\Auth\Middleware\AdminLogin::class,
执行命令行
php artisan make:middleware AdminLogin
打开创建的文件
(如果session中没有user,返回登录界面)
public function handle($request, Closure $next)
{
if(!session('user')){
return redirect('admin/login');
}
return $next($request);
}
14.验证器
值是通过$input传来的
$message可以不设置(注:下面有两处$message),不设置默认返回英文的错误
表单处的确认密码name='pwd.confirmation'
$rules = [
'pwd'=>'required|confirmed|between:6,20',
];
$message = [
'pwd.required'=>'密码不能为空',
'pwd.confirmed'=>'两个密码不一样',
'pwd.between'=>'位数不一致',
];
$validator = $validator::make($input,$rules,$message);
if($validator->passes()){
//执行修改密码操作
return back()->with('errors','修改密码成功');//返回字符串
}else{
//打印所有的错误
//dd($validator->errors()->all());
//返回错误
return back()->withErrors($validator);//返回对象
}
15 .页面返回错误
接14
@if(count($errors)>0)
@if(is_object($errors))
@foreach($errors->all() as $error)
{{$error}}
@endforeach
@else
{{$errors}}
@endif
@endif
16.传参到页面
$category = Category::all();
return view('admin.category.index')->with('data',$category);
return view('admin.category.index',compact('category'));
17.多级分类
18.创建资源类型
php artisan make:controller UserController --resource
19.ajax异步修改
值改变时,异步修改
view:
onchange = "changOrder(this,$this->id)"
function changeOrder(obj,$id){
var cate_order = $(obj).val();
$.post("{{url('admin/cate/changeorder')}}",{'_token':'{{csrf_token()}}','id':$id,'cate_order':cate_order},function(data){
if(data.status==1){
alert(data.msg);
}else{
alert(data.msg);
}
});
}
admin/cate/changeorder
public function changeorder(){
$input = Input::all();
$cate = Category::find($input['id']);
$cate->order = $input['cate_order'];
$re = $cate->update();
if($re){
$data=[
'status'=>1,
'msg'=>'更新成功',
];
}else{
$data=[
'status'=>0,
'msg'=>'更新失败',
];
}
return $data;
}
20.添加分类
$input需要的值:除token,直接入库
$input = Input::except('_token');
$re = Category::create($input);
laravel对写入有保护,需在模型中修改
//设置不能填充的数
protected $guarded = [];
21.修改分类
http://localhost/blog/public/admin/category/1/edit
public fuction edit($id)
{
$find = Category::find($id);
}
下拉框
@foreach
本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。
【上一篇】:没有了
0/500
Share your thoughts respectfully.