Important things one must know about cakephp
Cakephp is one of the most popular frameworks for PHP development. Cakephp provides an extensible architecture for developing, maintaining, and deploying applications. It is used for building dynamic applications as well as database driven website and E-commerce applications.
Here are some important things you should know about cakephp..
Here are some important things you should know about cakephp..
Easily creating static pages
Enter the pages controller – simply create a view inside the views/pages/ folder and it’ll automatically be rendered in /pages. For example, if created /views/pages/matt.thtml it would be accessible via http://www.example.com/pages/matt
Enter the pages controller – simply create a view inside the views/pages/ folder and it’ll automatically be rendered in /pages. For example, if created /views/pages/matt.thtml it would be accessible via http://www.example.com/pages/matt
Static pages – Adjusting the page title
If you’re using the pages controller and you need to change the page title, add the following to your view:
<? $this->pageTitle = ‘Title of your page.’; ?>
If you’re using the pages controller and you need to change the page title, add the following to your view:
<? $this->pageTitle = ‘Title of your page.’; ?>
Static pages – Adjusting other data sent to the layout
If you need to send data to the layout (such as a variable indicating what section to highlight on the nav bar), add this to your view:
<? $this->_viewVars['somedata'] = array(‘some’,'data’); ?>
That array should then be accessible as $some data inside your layout.
If you need to send data to the layout (such as a variable indicating what section to highlight on the nav bar), add this to your view:
<? $this->_viewVars['somedata'] = array(‘some’,'data’); ?>
That array should then be accessible as $some data inside your layout.
Creating a simple admin center
If you need to create an administrative back-end for your CakePHP site and would like all the actions with administrative capabilities to exist under a specific folder, open up config/core.php and uncomment: define(‘CAKE_ADMIN’, ‘admin’);
This will then make all actions that are prefixed with “admin_” to be accessible via:
/admin/yourcontroller/youraction. For instance, if created an action in posts controller called “admin_add,” you would access this via: www.example.com/admin/posts/add
From there simply password the admin folder to prohibit unwanted users from adding posts.
If you need to create an administrative back-end for your CakePHP site and would like all the actions with administrative capabilities to exist under a specific folder, open up config/core.php and uncomment: define(‘CAKE_ADMIN’, ‘admin’);
This will then make all actions that are prefixed with “admin_” to be accessible via:
/admin/yourcontroller/youraction. For instance, if created an action in posts controller called “admin_add,” you would access this via: www.example.com/admin/posts/add
From there simply password the admin folder to prohibit unwanted users from adding posts.
Viewing the SQL queries that are running behind the scenes
You can easily see the SQL queries that CakePHP is running by adjusting the DEBUG constant in config/core.php. 0 is production, 1 is development, 2 is full debug with SQL, and 3 is full debug with SQL and dump of the current object. You have to debug set at 2, which renders a table at the bottom of the page that contains SQL debug information.
If rendering a table at the bottom of your site is constantly breaking your layout during development (especially if you’re making AJAX calls and you’re getting SQL inside your pages, not just the bottom), you can easily style this table to be hidden by adding this to your CSS:
.cakeSqlLog { display: none; }
This will allow you to view debug information in the HTML source code without your layout getting mangled, just don’t forget to set debug back to 0 when your site goes live.
You can easily see the SQL queries that CakePHP is running by adjusting the DEBUG constant in config/core.php. 0 is production, 1 is development, 2 is full debug with SQL, and 3 is full debug with SQL and dump of the current object. You have to debug set at 2, which renders a table at the bottom of the page that contains SQL debug information.
If rendering a table at the bottom of your site is constantly breaking your layout during development (especially if you’re making AJAX calls and you’re getting SQL inside your pages, not just the bottom), you can easily style this table to be hidden by adding this to your CSS:
.cakeSqlLog { display: none; }
This will allow you to view debug information in the HTML source code without your layout getting mangled, just don’t forget to set debug back to 0 when your site goes live.
Multiple sources of documentation
Don’t just rely on the manual. The wiki and the API are invaluable sources of information. The tutorials in the wiki are especially useful, and the API may be daunting at first, but you’ll quickly find the information in there is crucial to building a site with CakePHP.
Don’t just rely on the manual. The wiki and the API are invaluable sources of information. The tutorials in the wiki are especially useful, and the API may be daunting at first, but you’ll quickly find the information in there is crucial to building a site with CakePHP.
Using bake.php
Bake is a command line PHP script that will automagically generate a model, controller, and views based on the design of your database. It is highly recommend using scaffolding to get a prototype going of a table that may change a lot in the beginning. If you’re fairly certain the data is not subject to any drastic change, I recommend using bake instead. With bake all the files are generated and written to disk and you can make modifications from there. It saves a lot of time doing the repetitive tasks such as creating associations, views, and the basic CRUD controller operations.
Using bake is really easy. Once you have a table(s) in your database created, change directories to the /cake/scripts/ folder and run:
php bake.php
If you choose to bake interactively it’ll walk you through the steps required to create your model, controller, and views. Once everything has been baked I usually go through all the generated code and make custom modifications as needed.
Bake is a command line PHP script that will automagically generate a model, controller, and views based on the design of your database. It is highly recommend using scaffolding to get a prototype going of a table that may change a lot in the beginning. If you’re fairly certain the data is not subject to any drastic change, I recommend using bake instead. With bake all the files are generated and written to disk and you can make modifications from there. It saves a lot of time doing the repetitive tasks such as creating associations, views, and the basic CRUD controller operations.
Using bake is really easy. Once you have a table(s) in your database created, change directories to the /cake/scripts/ folder and run:
php bake.php
If you choose to bake interactively it’ll walk you through the steps required to create your model, controller, and views. Once everything has been baked I usually go through all the generated code and make custom modifications as needed.
Logging errors
$this->log(‘Something broke’);
This will log your error to /tmp/logs/
$this->log(‘Something broke’);
This will log your error to /tmp/logs/
Creating a controller that uses other models Suppose you have a controller that needs data from a bunch of different models, simply add this to the top of your controller:
class yourController extends AppController
{
var $uses = array(‘Post’,'User’);
}
This controller would then have access to both the Post and the User model.
class yourController extends AppController
{
var $uses = array(‘Post’,'User’);
}
This controller would then have access to both the Post and the User model.
Call exit() after redirecting
This should be no surprise to anyone who has done any serious web development in the past, but make sure you call exit() after running $this->redirect() if there’s code afterward that you don’t want to run.
This should be no surprise to anyone who has done any serious web development in the past, but make sure you call exit() after running $this->redirect() if there’s code afterward that you don’t want to run.
Advanced model functions
Unless you delve in to the API, there are some very useful model functions at your disposal you might not know exist. I highly recommend reading over the Model Class Reference at least once. Here’s a few key functions –
» generateList() – I use this function primarily to populate select boxes with data from associated tables
» findBySql() – Sometimes you just need to write your own SQL
» findCount() – Returns number of rows matching given SQL condition
» hasAny() – Returns true if a record that meets the given conditions exists.
Unless you delve in to the API, there are some very useful model functions at your disposal you might not know exist. I highly recommend reading over the Model Class Reference at least once. Here’s a few key functions –
» generateList() – I use this function primarily to populate select boxes with data from associated tables
» findBySql() – Sometimes you just need to write your own SQL
» findCount() – Returns number of rows matching given SQL condition
» hasAny() – Returns true if a record that meets the given conditions exists.
Inserting logic before or after controller functions
Suppose you needed an array of colors to be available to every view rendered by your controller but you don’t want to have to define this data in every action. Using the beforeRender() callback will allow you to do this:
function beforeRender() {
$this->set(‘colors’,array(‘red’,'blue’,'green’);
}
This would make $colors accessible in every view rendered by that controller. beforeRender() is called after the controller logic and just before a view is rendered.
There’s also beforeFilter() and afterFilter(), which are called before and after every controller action. For more information, read up on callbacks in the models section of the manual.
Suppose you needed an array of colors to be available to every view rendered by your controller but you don’t want to have to define this data in every action. Using the beforeRender() callback will allow you to do this:
function beforeRender() {
$this->set(‘colors’,array(‘red’,'blue’,'green’);
}
This would make $colors accessible in every view rendered by that controller. beforeRender() is called after the controller logic and just before a view is rendered.
There’s also beforeFilter() and afterFilter(), which are called before and after every controller action. For more information, read up on callbacks in the models section of the manual.
Sending email Go through these tutorials Sending email and Sending email with PHPMailer
It is highly recommend the latter of the two, sending emails with PHPMailer is more secure and there’s less of a headache because you don’t have to deal with constructing the mail headers yourself.
It is highly recommend the latter of the two, sending emails with PHPMailer is more secure and there’s less of a headache because you don’t have to deal with constructing the mail headers yourself.
Creating a custom 404 error page
If you need to change the page that users see when a document is not found, create: /app/views/errors/error404.thtml
Source: http://cake-php.blogspot.com/2006/09/21-things-you-must-know-about-cakephp.html
Biztech Consultancy is a leading Cakephp Development Company based in India. Our core competency lies in cakephp web development and php development. Hire cakephp developers from us to avail the best services.
If you need to change the page that users see when a document is not found, create: /app/views/errors/error404.thtml
Source: http://cake-php.blogspot.com/2006/09/21-things-you-must-know-about-cakephp.html
Biztech Consultancy is a leading Cakephp Development Company based in India. Our core competency lies in cakephp web development and php development. Hire cakephp developers from us to avail the best services.
To get free consultation and project proposal regarding any Web Development Requirements, contact us at sales@biztechconsultancy.com. You can also keep in touch with us through your mobile at our Biztech Mobile Site
Our Industry base solution
- Biztech Consultancy comes up with a mobile version of its Website
- Why go for Service Oriented Architecture?
- Everything you need to know about Service Oriented Architecture (SOA)
- Get affordable Microsoft SharePoint Development Services from Biztech Consultancy
- Biztech all set to enter Microsoft SharePoint Development Arena




















