📜 ⬆️ ⬇️

Tips & tricks CakePHP # 2

In connection with the release of pre-beta 1.2 second edition of tips & tricks. We continue to acquaint you with the ideas and problems of version 1.2, the features of pre-beta, which we met in the process of developing a social network.
In addition, we started a blog on Habré - join, ask questions. I think we have something to discuss.

New core.php!


The most important pre-beta change is the core.php file format! Be sure to replace this file with the update and customize it at your discretion. In principle, the entire description of the parameters is already there.
By the way, in database.php it worked fine
'encoding' => 'utf8'

And no excuse for you with AppModel or mysql.php;)

Sanitize-> clean


Finally, the Sanitize - clean method resulted in a normal view. Previously, he was uncontrollable and cut everything that is possible in arrays. Now the method has parameters. However, in the first version, the odd_spaces filter did not work in the original pre-beta version due to a flaw, but the problem was solved thanks to the author of the article and the ticket system;)
So, now it is easy to configure filters for cleaning arrays of garbage.
Consider an example:
uses ('sanitize');
$ sanitize = new Sanitize ();
$ options = array (
'connection' => 'default',
'odd_spaces' => true
'encode' => false,
'dollar' => true
'carriage' => true
'unicode' => true
'escape' => false,
'backslash' => true
);
$ array = $ sanitize-> clean ($ array, $ options);


That's it, it's simple. Set the parameters to be used as true, and the rest as false.
By the way, since we started talking about Sanitase, I will offer you a rather logical construction that quickly and simply protects the site.
')

Sanitize in Appcontroller


Since all the incoming data from the forms comes through $ this-> data, and there is an Appcontroller add-on above all controllers, it is logical to scroll the following trick in beforeFilter:
class AppController extends Controller
{
var $ components = array ('RequestHandler');
var $ helpers = array ('javascript', 'ajax', 'navigation');

function beforeFilter () {
if (! empty ($ this-> data)) {
uses ('sanitize');
$ sanitize = new Sanitize ();
$ options = array (
'connection' => 'default',
'odd_spaces' => true
'encode' => false,
'dollar' => true
'carriage' => true
'unicode' => true
'escape' => false,
'backslash' => true
);
$ this-> data = $ sanitize-> clean ($ this-> data, $ options);
}
}
}

Everything, now all the data from the forms are cut automatically;) True, here comes the new feature of version 1.2 - named arguments.

Named arguments


A new feature named args is a way to transfer information via GET requests, i.e. through the query string using the router. Some simple variables that you need in any case can now be passed as follows:
cakephp.org/posts/index/page : 2 / sort: title
Accordingly, we have somewhere to create page variables with a value of 2 and sort with a title value.
It turns out that they are very easy to get from the controller:
$ this-> passedArgs ['page'] ;.
$ this-> passedArgs ['sort'];


Here, however, there is a problem that I have not yet investigated - what can be transmitted in this way? Is this a weak spot in defense? If yes, then passedArgs also makes sense to shove sanitize so that it thoroughly cleans these arguments :)

Set class


This handy feature will help in one line to parse a whole array, for example, $ this-> data.
Suppose you got a users table from the database. Accordingly, the data array looks like this:
$ users = array
(
0 => array
(
'User' => array
(
'id' => 1
, 'name' => 'Felix'
)
)
, 1 => array
(
'User' => array
(
'id' => 2
, 'name' => 'Bob'
)
)
, 2 => array
(
'User' => array
(
'id' => 3
, 'name' => 'Jim'
)
)
);


Want to get only usernames? Yes, easily:
$ userNames = Set :: extract ($ users, '{n} .User.name');


As you can see, {n} is a scalar array identifier. Accordingly, {n} can be placed in different places of the line, changing the location of this scalar array in the hierarchy of the array containing the scalar.
The class itself can be viewed separately, since the case is not limited to extract alone. There is merge, diff, contains, normalize. In general, as always, there is not enough help;), and the API here:
trac.cakephp.org/browser/branches/1.2.xx/cake/libs/set.php

Manual


Taki manual for 1.2 was back in place. True, there is still a lot of under-printed, but, apparently, it is constantly supplemented.
In open access in the html-form you can read here:
tempdocs.cakephp.org

We are waiting for your comments and suggestions. Also remember that the problems found can be left in the form of a ticket on the Kake website.

Source: https://habr.com/ru/post/16907/


All Articles