Category Archives: CakePHP

CakePHP

CakePHP: Set-Unset-Automate layout for page

There are some cases where we need different layouts for different pages as per out requireent. CakePHP provides by default a layout (app/View/Layouts/default.ctp). Case 1) If you don’t want any layout for a controller or any action, then we can use controller property $layout.

Case 2) If you want to have different layout for… Read More »

CakePHP: Set page title

As of 2.0 version, the attribute $pageTitle no longer exists. Use set() to set the title in any controller action like below:

You can check the default.ctp, you will find $title_for_layout for title.

CakePHP: Validating non database forms

There are many applications which does not require any database. In those applications, we need to validate forms. Lets see how CakePHP does this task. Lets create a contact us form.

Create contact us form

Now validation check in controller like below:

That’s all!

CakePHP: Auth Component – Authentication

Authentication is the process of identifying users by provided credentials and ensuring that users are who they say they are. CakePHP provides Auth Component for user authentication. Lets start using auth component by creating a plugin “Users”. First create a table “users” in your database.

Now create app model app/Plugin/Users/Model/usersAppModel.php

Now create model… Read More »

CakePHP: Configure Plugin Routes

In any application, we prefer to create modules which are accessible using routes in order to have manageable code. CakePHP allows us to create modules in the form of plugins. Lets create a plugin “Users”. Now create Controllers, Models, Views and Configuration files like in default application. Create app/Plugin/Users/Controller/UsersAppController.php if you need to access plugin… Read More »

CakePHP: Flash messages and Customization

There are some cases when we need to display one time notification message to the user after processing a request. In CakePHP, we call this “flash-messages”. We can set and display flash messages with the SessionComponent. Set Flash Message To set a message, use setFlash:

Now, you can display flash message in view file… Read More »

CakePHP: Configure multiple databases

There are many applications which uses multiple databases. CakePHP configures multiple databases easily. First thing you will need to do is set up all databases configurations in app/Config/database.php like below:

Now, if your tables in both databases are different, then you can simply create models for those tables. But by default, CakePHP picks up… Read More »

CakePHP: Call an action of another controller from one controller

CakePHP provides a way to call a controller’s method from any location. requestAction can be used to return rendered views or fetch the return value from controller actions. Lets assume we have two controllers: IndexController and PagesController

Also, you can return a view also like below: Create a view app/View/Pages/demo.ctp with content as… Read More »

CakePHP: View Helper Callbacks

CakePHP provides view helper callbacks to perform some complex logic. beforeRenderFile($viewFile) Is called before each view file(elements, views, parent views and layouts) is rendered. afterRenderFile($viewFile, $content) Is called after each view file(elements, views, parent views and layouts) is rendered. beforeRender($viewFile) The beforeRender method is called after the controller’s beforeRender method but before the controller renders… Read More »

CakePHP: Creating View Helpers

There are some situations where we need presentational logic to be shared between many views, elements, or layouts. For that purpose, CakePHP provides view helpers. Some built-in helpers are: CacheHelper,FormHelper,HtmlHelper,JsHelper,NumberHelper,Paginator,RSS,SessionHelper,TextHelper,TimeHelper, etc. Using and Configuring Helpers Each controller has a $helpers property that lists the helpers to be made available in the view. To enable a… Read More »