Heim  >  Artikel  >  Backend-Entwicklung  >  tp5与tp3的区别

tp5与tp3的区别

巴扎黑
巴扎黑Original
2016-11-22 14:29:224360Durchsuche

TP5作了很大的改变,更加灵活,没那么臃肿了,加入了一些令人振奋的功能,例如php7,unittest等,但是很多TP3下养成的习惯,在tp5可能要改改了。 

1 路由的变化 
tp3中定义路由 

Java代码  

'URL_ROUTE_RULES' => array( //定义路由规则  

        '/^password(\/)*$/'               => '/Admin/Auth/password',  

    ),  



这样用http://xx.com/password 和http://xx.com/Admin/Auth/password 
都可以访问到同一个地址http://xx.com/Admin/Auth/password 

而在tp5中是不行的,见Route.php 1251行 

Java代码  

if (isset(self::$rules['name'][$name]) || isset(self::$rules['name'][$name2])) {  

                throw new HttpException(404, 'invalid request:' . str_replace('|', $depr, $url));  

            }  



如果在路由中定义了配置的url,则不允许直接访问,会抛出异常 

Java代码  

'password'            =>  ['admin/auth/password', ['method'=>'get']],  



用配置的路由访问是正常的,但是不会跳转到admin/auth/password 
http://xxx/password 

用http://xxx/admin/auth/password 访问会报错 

Java代码  

[0] HttpException in Route.php line 1252  

非法请求:admin/auth/password  

  

                $route = [$module, $controller, $action];  

                // 检查地址是否被定义过路由  

                $name  = strtolower($module . '/' . Loader::parseName($controller, 1) . '/' . $action);  

                $name2 = '';  

                if (empty($module) || isset($bind) && $module == $bind) {  

                    $name2 = strtolower(Loader::parseName($controller, 1) . '/' . $action);  

                }  

  

                if (isset(self::$rules['name'][$name]) || isset(self::$rules['name'][$name2])) {  

                    throw new HttpException(404, 'invalid request:' . str_replace('|', $depr, $url));  

                }  

            }  

            return ['type' => 'module', 'module' => $route];  

        }  



2 Model的find,select等方法返回的是Model对象,不是数据。可以用getData()获取原始数据。建议在Model中定义方法使用db()来查询数据,在将方法暴露给controller调用。 

3 foreach中如果用Model()->save();新增数据,一定要加上->isUpdate(false) 否则只有第一个会是insert,其它都成了update。这个估计是个bug。 


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn