TP5使用问题总结

1,把源码文件拷到目录中,需要运行localhost/public,才能显示欢迎页面,把web服务器的域名打开目录指向public文件夹,这样输入localhost就可以直接访问欢迎页

+1.关于TP5在不同的php版本下,出现No input file specified php5.6以上回出现这个问题,需要改的地方有两点

a.在入口文件目录下的  .htaccess 文件中把

RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

改成

RewriteRule ^(.*)$ index.php [L,E=PATH_INFO:$1]

b.在php.ini文件中

;always_populate_raw_post_data = -1

改成

always_populate_raw_post_data = -1

这样可以成功访问

2.关于模板渲染与模板传值问题

///////方法一 
        return view('index',['name'=>"thinkphp",'title'=>'首页']);
///////方法二
        $this->assign(['name'=>'thinkphp','title'=>"show"]);
        return $this->fetch();
///////方法三
        return $this->fetch('index',['name'=>'thinkphp','title'=>"首页"]);   
///////方法四
$this->assign(['name'=>'thinkphp','title'=>"show"]);

          return view();

如果不需要模板渲染直接传值可用return $this->display($content,$vars)

       //return $this->display($content,$vars);直接渲染内容,不经过模板
        $name="abc";
        $title="123";
        return $this->display("{$name}-{$title}");
*****结果为
    abc-123

 fetch与view可以临时改变输出替换

return $this->fetch('index',[],['__PUBLIC__'=>'/public/']);
return view('index',['name'=>'thinkphp'],['__PUBLIC__'=>'/public/']);

3.模板输出替换

5.0.4增加__ROOT__ __STATIC__ __JS____CSS__内置替换规则

__ROOT__:/

__STATIC__:/public/static

__JS__:/public/static/js

__CSS__:/public/static/css

后期开发需要全局替换的话,可以直接在配置文件中添加

'view_replace_str'  =>  [
    '__PUBLIC__'=>'/public/'
]

 4.TP5中return不能返回数组

方法1:在congfig配置文件中默认输出类型是html改成json即可

// 默认输出类型
    'default_return_type'    => 'json',

方法2.:把需要返回的数组转换成json格式

public function save_user(){
        $request = Request::instance();
        $data = $request->param();
        //dump($request->param());
        return json($data);
    }

猜你喜欢

转载自www.cnblogs.com/zhangdong156/p/9294647.html