::.UrCosme.::2009å¹´7æ??17æ?¥MAKE UP FOR EVERç¾?å¦?æ?°è?? | [æ´»å??] A BEAUTIFUL CHANGE ! MAKE UP FOR EVER 25é?±å¹´ç¾?éº?å·¨ç?» å¼µè?¸äº¬ç??å·´é»?ç??æ?³ é«?é©?è?±é?½è®?å¦?ç§?
Shared via AddThis
2009年7月19日 星期日
2007年7月28日 星期六
Redirect with exit
If you do a redirect in CakePHP 1.1 you often have to add an exit() after the redirect statement to stop the script execution:
$this->redirect('controller/action');
exit();
Thanks to a small enhancement in CakePHP 1.2 in the form of an additional parameter for the redirect() function you can now do:
$this->redirect('controller/action', null, true);
It’s the same functionality as in the previous code snippet, but with the advantage that your code becomes testable (you cannot test a function which contains an exit(), as the exit() also stops the execution of the test).
$this->redirect('controller/action');
exit();
Thanks to a small enhancement in CakePHP 1.2 in the form of an additional parameter for the redirect() function you can now do:
$this->redirect('controller/action', null, true);
It’s the same functionality as in the previous code snippet, but with the advantage that your code becomes testable (you cannot test a function which contains an exit(), as the exit() also stops the execution of the test).
Writing an installer for your CakePHP application
If you write an application which the user must install on his own server, you have to think about the installation process of your application. In this article I will describe the approach I have used in my application.
This approach is heavily inspired by the one used by WordPress. That means the user has to create the database and to define the database settings in app/config/database.php himself. When this is done, the user must run the installer, which then creates the tables and does some application-specific stuff like creating a default user.
The installer is a usual controller, which is automatically called when the user accesses the application for the first time. For this purpose I added a bit of logic to app/config/routes.php:
if (file_exists(TMP.'installed.txt')) {
// the routes for when the application has been installed
} else {
Router::connect('/:action', array('controller' => 'installer'));
}
So as long as there is no “installed.txt” file, the user gets the first page of the installer when he requests the url of the application. The consequence of this logic is that we have to create such a file at the end of the installation process (see below).
The installer controller itself is straight-forward, each action is one step in the installation process. You can find a minimal version of the installer below:
// app/controllers/
uses('model' . DS . 'connection_manager');
class InstallerController extends AppController {
var $uses = array();
function beforeFilter() {
if (file_exists(TMP.'installed.txt')) {
echo 'Application already installed. Remove app/config/installed.txt to reinstall the application';
exit();
}
}
function index() {
}
function database() {
$db = ConnectionManager::getDataSource('default');
if(!$db->isConnected()) {
echo 'Could not connect to database. Please check the settings in app/config/database.php and try again';
exit();
}
$this->__executeSQLScript($db, CONFIGS.'sql'.DS.'app.sql');
$this->redirect('/installer/thanks');
}
function thanks() {
file_put_contents(TMP.'installed.txt', date('Y-m-d, H:i:s'));
}
function __executeSQLScript($db, $fileName) {
$statements = file_get_contents($fileName);
$statements = explode(';', $statements);
foreach ($statements as $statement) {
if (trim($statement) != '') {
$db->query($statement);
}
}
}
}
It is a pragmatic solution with the drawback that it is not really reusable,..
This approach is heavily inspired by the one used by WordPress. That means the user has to create the database and to define the database settings in app/config/database.php himself. When this is done, the user must run the installer, which then creates the tables and does some application-specific stuff like creating a default user.
The installer is a usual controller, which is automatically called when the user accesses the application for the first time. For this purpose I added a bit of logic to app/config/routes.php:
if (file_exists(TMP.'installed.txt')) {
// the routes for when the application has been installed
} else {
Router::connect('/:action', array('controller' => 'installer'));
}
So as long as there is no “installed.txt” file, the user gets the first page of the installer when he requests the url of the application. The consequence of this logic is that we have to create such a file at the end of the installation process (see below).
The installer controller itself is straight-forward, each action is one step in the installation process. You can find a minimal version of the installer below:
// app/controllers/
uses('model' . DS . 'connection_manager');
class InstallerController extends AppController {
var $uses = array();
function beforeFilter() {
if (file_exists(TMP.'installed.txt')) {
echo 'Application already installed. Remove app/config/installed.txt to reinstall the application';
exit();
}
}
function index() {
}
function database() {
$db = ConnectionManager::getDataSource('default');
if(!$db->isConnected()) {
echo 'Could not connect to database. Please check the settings in app/config/database.php and try again';
exit();
}
$this->__executeSQLScript($db, CONFIGS.'sql'.DS.'app.sql');
$this->redirect('/installer/thanks');
}
function thanks() {
file_put_contents(TMP.'installed.txt', date('Y-m-d, H:i:s'));
}
function __executeSQLScript($db, $fileName) {
$statements = file_get_contents($fileName);
$statements = explode(';', $statements);
foreach ($statements as $statement) {
if (trim($statement) != '') {
$db->query($statement);
}
}
}
}
It is a pragmatic solution with the drawback that it is not really reusable,..
How to enable/disable debug messages for a certain action
An often asked question in the CakePHP IRC channel is: “How can I enable [or disable] the debug messages for a certain action?” The answer to this question is simple (works in both CakePHP 1.1 and 1.2):
function action() {
Configure::write('debug', '2');
// do something
}
Please notice that the key must be in lowercase, ‘DEBUG’ won’t have the desired effect.
function action() {
Configure::write('debug', '2');
// do something
}
Please notice that the key must be in lowercase, ‘DEBUG’ won’t have the desired effect.
Select distinct with CakePHP
The solution is rather simple:
$this->User->findAll(null, 'DISTINCT User.city');
Or with the array syntax:
$this->User->findAll(null, array('DISTINCT User.city'));
Please notice that the keyword “DISTINCT” has to be uppercase, else you will get the following error:
Unknown column 'distinct User.city'
$this->User->findAll(null, 'DISTINCT User.city');
Or with the array syntax:
$this->User->findAll(null, array('DISTINCT User.city'));
Please notice that the keyword “DISTINCT” has to be uppercase, else you will get the following error:
Unknown column 'distinct User.city'
訂閱:
意見 (Atom)