1.视图
view('home.test1')->with('name', 'Victoria');
;可以引用视图,引用路径为app esourcesviewshome est1.blade.php.
->with('name', 'Victoria');是给视图传递变量,也可以view('home.test1',$data)来传递变量,但是$data必须是关联数组,因为在模板中需要这样获取{{$key}}
一些blade模板引擎的常用标签:
if标签
@if (count($records) === 1) I have one record!
@elseif (count($records) > 1) I have multiple records!
@else I don't have any records!
@endif
@unless (Auth::check()) You are not signed in. @endunless
循环
@for ($i = 0; $i < 10; $i++) The current value is {{ $i }}
@endfor @foreach ($users as $user) <p>This is user {{ $user->id }}</p>
@endforeach
@while (true) <p>I'm looping forever.</p>
@endwhile
引用视图:
@include('view.name') 也可以给它传递数组@include('view.name', array('some'=>'data'));
在视图中使用函数{{app_path()}}就可以执行了,末尾不要有分号.有些内置的类在控制器开头要加的,在视图中可以省略
2.补充一个路由方面的知识,如果按笔记1里面的方法,那是不是每个方法都要给个路由,这样会很麻烦,所以laravel提供了注册控制器的方法:
Route::controllers([ 'auth' => 'AuthAuthController', 'password' => 'AuthPasswordController', 'hello'=>'HelloController' ]);
注册一个hello的控制器,然后里面的方法命名规范是请求方式比如get+方法名,方法名头字母大写,这样:getBox,这样域名/hello/box就可以访问到这个方法了,如果有这种命名方式比如getSayHey,那么路由里的方法名要say-hey才能识别。
3.cookie
laravel里的cookie机制比较特别,要结合响应函数来一起用,比如:
$cookies = Cookie::make('user',123,$minute); return Response::json(array('status'=>1,'info'=>'登陆成功'))->withCookie($cookies);
它必须在响应对象里加上withCookie($cookie);才能把cookie添加成功,还有forget函数也是如此,如果不想在响应时候添加cookie,可以Cookie::queue('name','value',1);来添加cookie,想要获取cookie,执行Cookie::get('name');就行了.
4.session
Session::put('key', 'value');添加session
Session::get('key');或者
session('key');
获取session 可以设置默认值:
Session::get('key', 'default');
Session::pull('key', 'default');//
从 Session 取回对象,并删除
Session::forget('key');删除session
Session::flush();清空
Session::all();
从 Session 取出所有对象