CakePHP 3.x or 2.x | Some important configuration settings

Author - Rita

Some important settings that can help you while working with CakePHP 3 or 2.

1. How to set Homepage for your website in CakePHP 3?

Go to the root directory of your project. You will see config folder there. Inside Config
folder, open routes.php file. Add below the line of code to the routes.php file:

$routes - > connect('/', ['controller' => 'Home', 'action' => 'index']);

In the above line of code “/” represents the base path, ‘Home’ is the controller and ‘index’ is the controller action which we want to call on startup.

2.  How to avoid authentication in some of your controller actions?

In most of the websites, there are certain web pages like terms and conditions, privacy statements etc which any one can view. Users do not need to login to view such information. So in such cases, we need to bypass authentication. To achieve this add below line of code in your controller:

public
function beforeFilter(Event $event){
parent::beforeFilter($event);
$this - > Auth - > allow(['index']);
}

The syntax for cake2 to bypass authentication in specific controllers is as below:

function beforeFilter(){
parent::beforeFilter($event);
$this - > Auth - > allow(array('index', 'another action'));
}

the before filter method executes at the very beginning while executing the request. So by adding $this->Auth->allow ( [ ‘index’ ] ); line inside this method will bypass authentication for the specified action.

3.  What is the best way to create url’s in your views?

While we create forms in our view, we have to specify action url there. So instead of writing hardcoded url, it’s better to use the built in features to create url dynamically. Let’s consider the below line of code:

"Controller Name ", "action " => "Action name " ] );?>">

So in the above piece of code you can see the action attribute of form $this->url->build is the default syntax provided by the cake php3 to create url’s. Controller Name is the name of the controller and action name is the name of the action. Keyword echo is mandatory to render the url in the action attribute.
In cake 2, We will use below syntax to create url’s:

echo $this - > Html - > url(array(
    "controller" => "posts",
    "action" => "search"));

4.  How to make an ajax request in cakephp 3?

When we write URLs for normal request response cycle following statement will be used:

$this - > Url - > build(["controller" => "Controller Name ", "action" => "Action name"]);

But if we are calling that particular controller action as ajax hit we need to add json at the end of actionname as below:

$this - > Url - > build(["controller" => "Controller Name ", "action" => "Action name.json "]);

Now this request will be treated as ajax request and does not search for any view. No other configurations are needed in this case. Same function will behave as normal action/ajax action. If action is called without ‘json’ extension then it is a normal request and renders view at the end. If we use .json extension at the end of action name then it will behave as ajax request. This is one of the better concept added in cakephp 3.
In cake2, we will follow the below syntax:

echo $this - > Html - > url(array(
    "controller" => "posts",
    "action" => "search.json"));

5.  How to allow ajax Requests in cakephp 3?

By default, all ajax requests are blocked in cake3 due to security purposes. To allow ajax requests inside every controller, You have to add below lines of code in each of your controller file:

public $components = array('Security', 'RequestHandler');
public
function beforeFilter(Event $event){
parent::beforeFilter($event);
$this - > Security - > config('unlockedActions', [‘Ajax Action here’]);
}

Here Security Component is the default component added by cake 3. We have to unlock all our ajax actions inside it to make them work in cake3.

In cake2 the syntax is different than cake3 as shown below:

public $components = array('Security', 'RequestHandler');
public
function beforeFilter(){
parent::beforeFilter();
$this - > Security - > unlockedActions = array('ajax_action');
}

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *