欢迎光临
感谢一路有你

laravel | 系统学习笔记二

如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
 
服务容器
	绑定(使用bind 或 singleton)
		public/indx.php下的
			$app = require_once __DIR__.'/../bootstrap/app.php';
		app.php 下(字符串绑定到对象中)
			$app->singleton(
			    Illuminate\Contracts\Http\Kernel::class,//字符串
			    App\Http\Kernel::class//对象
			);
	解析(使用make)
		$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
	依赖注入
		public function index(Illuminate\Http\Request $request, \Illuminate\Contracts\Logging\Log $log){
			//在参数中加入一个类  这个参数就会直接实例化这个类 , 直接可以使用
			$log->info("直接使用");
		}
服务提供者
	绑定一般由服务提供者来做
	服务提供注册
		public function register()//服务提供之前进行注册
		public function boot()//服务提供加载之后进行注册
	延迟服务提供
		protected $defer = true;
	提供2种写入方式
		1.config/app.php====>providers
		2.框架中写入的
门脸模式
	不需要new,直接静态调用
	public function index(Illuminate\Http\Request $request){
		dd($request->all());
	}
	//门脸模式
	public function index(){
		dd(\Rrequest::all());
	}
	解析在config/app.php
		aliases
        'Request' => Illuminate\Support\Facades\Request::class,
		打开当前门脸类
		\vendor\laravel\framework\src\Illuminate\Support\Facades
demo(服务容器/服务提供者/门脸)
	容器
		public function index(){
			$app = app();//获取容器
			<!--\vendor\laravel\framework\src\Illuminate\Log\LogServiceProvider.php
			public function register()
			{
			    $this->app->singleton('log', function () {
			        return $this->createLogger();
			    });
			} -->
			$log = $app-> make('log');//make('注册时的字符串')
			$log()->info('111');
		}
	门脸
		public function index(){
			\Log::info("1111");
		}
如何查找门脸/注入类 有哪些函数
	php artisan tinker
	app('Psr\Log\LoggerInterface'); // ==>app('log')
	查找方法
		https://laravel.com/api/5.4
		Illuminate\Log\Writer
用户注册/登陆
	//验证
	'name'=>'required|min:3|unique:users,name'//z在users表name唯一	
	密码加密 laravel默认(bcrypt()加密)
		$pwd = bcrypt(request('pwd'))
	use App\Model;//model
	use App\User;//controller
	$user = User::create(compact('name','email','password'));

AUTH门脸类
	登录
		if(Auth::attempt(['email'=>$email,'password'=>$password],$remember))
	登出
		Auth::logout();
	$user = Auth::user()//获取当前已通过认证的用户
	$id = Auth::id//已认证的用户id
	使用:
		<!-- model  User.php -->
		use Illuminate\Foundation\Auth\User as Authenticatable;
		class User extends Authenticatable
		{
			protected $fillable = [
				'name','email','password'
			]
		}
		<!-- conteoller -->
		$user = request(['email','password']);
		$is_remember = boolval(request('is_remember'));
		if(\Auth::attempt($user,$is_remember)){
			return redirect('/index');
		}
		return \Redirect::back()->withErrors("密码不匹配");//表单提交的上一页
	用户授权Policy
		定义策略类
		注册策略类和模型相关
		策略判断
			1.if($user->can('update',$post)){....}
			2.$this->authorize('update',$post);
			3.@can('update',$post)
				可操作
			  @elsecan('create',$post)
			  	可创建
			  @endcan
		demo
			文章中加入用户id
			$user_id = \Auth::id();
			$params = array_merge(request(['title','content']),compact("user_id"))
			$post = Post::create($params);

			页面当前登录用户
			{{\Auth::user()->name}}

			文章关联用户(model )
			class Post extends Model{
				public function user(){
					return $this->belongTo('App\User','user_id','id');//post表的外键user_id/user表的主键id
					return $this->belongTo('App\User');//如果遵循表_id  id ,后面两参数可不写
				}
			}
			页面中通过user_id获取用户名 {{$post->user->name}} // 不加括号  user不加括号返回关联关系对应的对象,加括号返回关联关系
			权限policy
				1.定义
					php artisan make:policy PostPolicy  //\app\Policies
				2.注册/关联
					//PostPolicy.php
						use App\User;
						use App\Post;
						//修改
						public function update(User $user,Post $post){
							return $user->id ==$post->user_id;
						}
						//删除
						public function delete(User $user,Post $post){
							return $user->id ==$post->user_id;
						}
					注册\app\Providers\AuthServiceProvider.php
						protected $policies = [
						    'App\Model' => 'App\Policies\PostPolicy',
						];
				3.策略判断
					<!-- controller -->
					$this->authorize("update",$post);
模型关联
	hasone 一对一(用户-手机号)
	hasMany一对多(文章-评论)
	belongsTo 一对多反向(评论-文章)
	belongsToMany 多对多(用户-用户)
	hasManyThrough 远层一对多(国家-作者-文章)
	morphMany 多态关联(文章/视频-评论)
	morphToMany 多态多对多(文章/评论-标签)

	demo 
		文章中的评论
			post.php (model)
				public function commons()
				{
					return $this->hasMany('App\Commons')->orderBy('created','desc');
				}
		评论所属的文章
			commons.php (model)
				public function(){
					return $this->belongsTo('App\Post');
				}

提交评论
model中是用APP的model  use App\Model
	use \App\commons;

	$comment = new Comment();
	$comment->user_id = \Auth::id();//当前用户id
	$comment->content = request('content');//评论的内容
	$post->commons()->save($comment);//保存
	return back();//返回上一页面

模型关联预加载
	1.$books = App\Book::with('author')->get();	//渲染之后架子啊
	2.$books = load('author','punlisher'); //推荐,渲染之前加载
		public function show(Post $post){
			$post->load('comments');//controller
		}
		@foreach($post->comments as $comment)
			{{$comment->name}}
		@endforeach

模型关联计数
	$num = App\Post::withCount('comments')->get
		demo
			$posts = Post::orderBy('created','desc')->withCount("comments")->paginate(6);
			{{$post->comments_count}}

点赞
	//赞
	public function zam(Post $post){
		$param = [
			'user_id' => \Auth::id(),
			'post_id'=>$post->id
		];
		Zan::firstOrCreate($param);//判断数据库是否有此数据,如果有,则读取,如果没有则创建
		return back();
	}
	//取消赞
		用户--赞关联
		//post model 
		public function zan($user_id){
			return $this->hasOne(\App\Zan::class)->where('user_id',$user_id);
		}

		//controller
		public function unzan(Post $post){
			$post->zan(\Auth::id())->delete();
			return back;
		}

	页面
		@if($post->zan(\Auth::id())->exist())//判断是否已经存在
		..
		@else
		..
		@endif

withCount扩展  
	withCount(模型关联的函数名)

	$posts = Post::orderBy('created','desc')->withCount(['comments',zans])->paginate(6);
	{{$post->comments_count}}
	{{$post->zans_count}}


laravel项目开发规范
	https://laravel-china.org/docs/laravel-specification/5.5

赞(0) 打赏
未经允许不得转载:王明昌博客 » laravel | 系统学习笔记二
分享到: 更多 (0)

相关推荐

  • 暂无文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

×
订阅图标按钮