未加星标

CakePHP Tips Fall 2016

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
Always use proper version check method

Be aware of version checks that are using the inferior and even wrong < / > comparison.

This can lead to wrong conditions with higher minor versions.

I had this in my code from the early 2.x days:

if ((float)Configure::version() < 2.4) { ... }

With the upcoming 2.10 release this will now suddenly start to return true. Luckily my 2.next tests already revealed that and I was able to fix it up properly:

if (version_compare(Configure::version(), '2.4') < 0) { ... }

This will now work in all minors of 2.x.

The same is true for 3.x, of course, even though we are "only" at 3.3.6 at this point in time yet.

Order your flash messages semantically

You can use the Flash plugin to enhance your flash messages a bit.

It works out of the box with your current setup, but adds a few useful things on top:

Prevent size and output issues when running into a redirect loop boiling up messages in the session (>> 50) by limiting the stack to 10 by default. Use ordered output (error first, then warning, success and lastly info). Errors should be visible first in case multiple types are to be displayed. Switch away from basic log files

I would switch away from basic log files to database even if it is just a simple SQLite one like DebugKit uses.

This way you can more easily navigate and filter through them.

Check out the DatabaseLog plugin which can do exactly that in just a few minutes of configuration.

For not just personal fun apps it is advised to look into a bit more sophisticated approaches, like using Monolog and services like NewRelic to write to. But for smaller projects it can be enough to have a small admin backend to filter through the logs, especially error and warning types.

Keep your controller code lean

That specifically includes all those verbose allow statements to make certain pages public:

use Cake\Event\Event; ... /** * @return void */ public function beforeFilter(Event $event) { parent::beforeFilter($event); $this->Auth->allow('index'); }

See TinyAuth which since recently allows to more cleanly declare this with a single INI file in your src/config folder.

The complete code above goes away for every controller

As long as not needed (custom dynamically made decisions based on session or alike) this kind of noise should be kept out of the controllers.

This makes it also possible to modify the public access to actions without the need to be a developer. The next step could be to make the input for TinyAuth database-driven and alike.

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: PHPSQL
分页:12
转载请注明
本文标题:CakePHP Tips Fall 2016
本站链接:http://www.codesec.net/view/484834.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(31)