美文网首页
laravel随笔记

laravel随笔记

作者: 散装咖啡 | 来源:发表于2017-03-23 00:16 被阅读4次

    session篇

    Session::set('key', 'value');//value的值可以是数组

    Session::save();//需要执行这个才是保存

    Session::forget('key');//删除session

    事务篇

    \DB::beginTransaction();

    try{

    $data=$this->UserInfos($openId);

    if($data['status'] =='fail') {

    //Exception类接收的参数  $message = "", $code = 0, Exception $previous = null

    throw new\Exception("1");

    }

    \DB::commit();

    }catch(\Exception$e){

    \DB::rollback();//事务回滚

    }

    容器类

    class Container{

    public $bindings;

    public function bind($abstract, $concrete){

    $this->bindings[$abstract]=$concrete;    

    }

    public function make($abstract, $parameters=[]){

    return call_user_func_array($this->bindings[$abstract], $parameters);    

    }

    }

    服务注册(绑定)

    $container=new Container();

    $container->bind('db',function($arg1,$arg2){

    returnnewDB($arg1,$arg2);});

    $container->bind('session',function($arg1,$arg2){

    returnnewSession($arg1,$arg2);});

    $container->bind('fs',function($arg1,$arg2){

    returnnewFileSystem($arg1,$arg2);

    });

    容器依赖

    class Writer{

    protected $_db;

    protected $_filesystem;

    protected $_session;

    protected $container;

    public function Writer(Container $container){

    $this->_db=$container->make('db',[1,2]);

    $this->_filesystem=$container->make('session',[3,4]);

    $this->_session=$container->make('fs',[5,6]);    }}

    $writer=newWriter($container);

    跨域

    /**

    * Created by PhpStorm.

    * User: root

    * Date: 2017/5/4

    * Time: 20:20

    */

    namespaceApp\Http\Middleware;

    useClosure;

    classCors

    {

    private$doman='*';

    private$headersType='Origin, Content-Type, Cookie, Accept, multipart/form-data, application/json';

    private$methods='GET, POST, PATCH, PUT, OPTIONS';

    private$credentials=false;//如果域名是*,这个不能是true

    public functionhandle($request, Closure$next)

    {

    header('Access-Control-Allow-Origin: '.$this->doman);

    header('Access-Control-Allow-Headers: '.$this->headersType);

    header('Access-Control-Allow-Methods: '.$this->methods);

    header('Access-Control-Allow-Credentials: '.$this->credentials);

    $response=$next($request);

    return$response;

    }

    }

    然后, 添加中间件的时候,需要添加全局才生效

    相关文章

      网友评论

          本文标题:laravel随笔记

          本文链接:https://www.haomeiwen.com/subject/jonanttx.html