laravel 笔记

1.路由上下关系影响接收状况
/test
/{name} /test送到/test中

/{name}
/test /test送到/{name}中


2.重定向+传送数据Redirect::to
return Redirect::to("test")->with("params",$name);
在test中用Session::get("params")接收params参数

3.Redirect::route
回传数据
return Redirect::route("test",array("num"=>10));
return Redirect::action('UserController@profile', array("num" => 1));
接收使用Input::get("num");

4.在路由中写
View::share('name', 'Steve');
在所有模板页面中都可以调用

5.获取子模板中的内容,并填充到指定模板的位置
View::make("hello")->nest("自定义名称","子模板名称",array(子模板所需的数据));
在模板hello.php中指定位置输出‘自定义名称’就行了;

6.向控制器传参数
路由:Route::get('/{name?}', "HomeController@showWelcome")->where("name","[w]+");
控制器:public function showWelcome($name=null){ ... }

7.1 Route::controller("参数名","控制器名");的作用
例子:
路由页面中
Route::controller("demo","DemoController");
访问DemoController控制器getLogin方法 http://192.168.128.100/demo/login

Route::controller('demo','DemoController',array(
'getIndex' => 'demo.index', //控制器中getIndex($params)有参数,访问方式为http://192.168.128.100/demo/index/参数
'getLogin' => 'demo.login'
));
注:参考《http://ofcss.com/2014/12/16/two-tips-about-laravel-4.html》

DemoController控制器中
public function anyIndex(){} Index是默认的接收方法,三种变体anyIndex,getIndex,postIndex接收
方法名:接收方式+名称 any=(get,post....)
public function getProfile() url:http://192.168.128.100:8080/public/demo/profile
总结:Router::controller设定指向的控制器,里面的方法名一般为:访问方式(get,post,put,delete..)+ 自定义名称,默认访问的是index方法
public function getIndex()
public function postIndex()

7.2 Route::resource("参数名","控制器名称");
Route::resource('demo','DemoController')
被Router::resource设定指向的控制器,里面的方法名一般默认的有index,create,store,show,edit,save,destroy,其他的可以根据需求自定义,而且不用加水请求方式

请求方法 请求URI 自动对应的控制器方法 代表的意义
GET /article index 索引/列表
GET /article/create create 创建(显示表单)
GET /article/{id} show 显示对应id的内容
GET /article/{id}/edit edit 编辑(显示表单)
POST /article store 保存你创建的数据
PUT/PATCH /article/{id} save 保存你编辑的数据
DELETE /article/{id} destroy 删除

注:参考《https://phphub.org/topics/688》

8.cookie
获取:Cookie::get('name')
添加:$response = Response::make('Hello World');

9.控制器中调用model
模型Demo.php
class Demo extends Eloquent{ }
1.控制器中
$demo = new Demo();
$demo->方法();
2.$result = with( new Demo )->模型方法();

10.获取插入的数据的id
public $incrementing = true;可以不写,默认为true,但是为false就获取不了了,这是禁止主键递增的
在模型中
$demo = new Demo();
$demo->name = 'three';
$demo->age = '3';
$demo->save();
dd($demo->id);

11.属性$fillable,$guarded的作用
这两个作用是管理某些字段在create中是否会被插入到表中。create是插入新的数据到表中

在create时,如果不在模型类中声明这两个属性,是会报错的
只有在$fillable数组中的字段,才能被插入到表中,如果某些字段在guarded中,则不会被插入到表中,即时他在create中

注意:当有100多个字段时,都要被插入到表中,不可能都一个个写入到fillable中,这时可以不写fillable,把guarded=array()

原文地址:https://www.cnblogs.com/hanyouchun/p/4460392.html