Showing posts with label php programming language. Show all posts
Showing posts with label php programming language. Show all posts

Saturday, 13 December 2014

Bad CakePHP Habits & How to Rectify Them!

As a programmer we are always learning, so it is important to follow the best practices and adjust to them.

CakePHP Conventions
There are actually CakePHP coding conventions which should be followed. I will highlight a few things,

1) Control Structures.  So often you see programmers get this wrong, and in even some cases bring practices for other coding languages.  CakePHP expects the following syntax:

if ((expr_1) || (expr_2)) {
// action_1;
} elseif (!(expr_3) && (expr_4)) {
// action_2;
} else {
// default_action;
}

In the control structures there should be 1 (one) space before the first parenthesis and 1 (one) space between the last parenthesis and the opening bracket.  So this means that the following is incorrect:

if ($foo) {
$bar = true;
if ($action) {
$to = false;
}

The indentation needs to line up correctly.

I often hear programmers say “but I am too busy to make the code neat….” My response is – “trust me, neat code will stand the test of time”.  Writing code which isn’t readable will be a nightmare to come back to if you need to make a change in a few months.

Fat Models, Skinny Controllers

Good CakePHP code will have the logic in the model files. This takes a bit to get used to, but once mastered there is no looking back!  A controller file should be used for what it is intended for in the MVC pattern – controlling!  So use your controller file to handle user actions, while let the code logic go in the model file.



The default add function is as follows:

public function add() {
if ($this->request->is(‘post’)) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__(‘Your post has been saved.’));
return $this->redirect(array(‘action’ => ‘index’));
}
$this->Session->setFlash(__(‘Unable to add your post.’));
}
}

This controller action is fine for a simple add, but what would happen if you wanted to do things such as send an email to the admin when a post was added, or update another model association when a post was added.  This is additional logic, but this logic shouldn’t go into our controller file.

Instead we would write a function for this in our App.php model, perhaps something like this:

public function addPost($data = array(), $emailAdmin = true) {
$this->create();
$this->save($data);
// update any other tables
// send the email to the admin user
if ($emailAdmin) {
}
// if all is successful
return true;
}

This would then result in a small change to the controller action as follows:

public function add() {
if ($this->request->is(‘post’)) {
if ($this->Post->addPost($this->request->data)) {
$this->Session->setFlash(__(‘Your post has been saved.’));
return $this->redirect(array(‘action’ => ‘index’));
}
$this->Session->setFlash(__(‘Unable to add your post.’));
}
}

As you can see, the new action is actually one less line, because the $this->Post->create() has been moved to the model file.
This is a perfect, everyday example of where moving logic to the model file is a good idea – and it certainly makes for a much cleaner code base!

Source: CodeWebber

Thursday, 11 December 2014

Best Ways to Create an Awesome Web Application

Web applicationIt is an established fact that PHP is one of the most popular language for programming. Sometimes, the PHP developers are likely to rely on the language heavily and, as an end result many mistakes can occur in the plan. These five ways are here to help you to avoid the possible troubles of this kind-

Read the reports around the errors

The error reporting can be your electronic consultant. It will reveal the mistakes in programming, so you are able to correct them at once. Still if you ignore the reports, the errors are going to be visible for the site users through a similar error report plan. They will get acquainted with your site, including the truth you’d possibly want to hide.

The “Bad features” on the PHP shall be powered down

Wide range of features are there in PHP to make the life of the world-wide-web application developer less difficult. These PHP applications are classified as the real miracle from time to time, still some ones give the unwanted consequences at the same time. With their help the bugs can enter your script and also the secret data may be revealed. So secure your bad applications before you start working with this system.

The input will likely be validated properly

When you crave to protect computer data, try to confirm the input. In the event that you’re experienced developer, you most likely know what exactly shall appear on the input. With this you will end up sure the guests can get the access only to the approved information.

The cross internet site scripting attack can appear in the user input

The application can accept the input from the user. It may come in various forms towards web application. Ensure the users tend not to download some dangerous content of one’s site through your input. Keep your site secure.

The SQL Treatments shall not possibly be possible

The SQL Injection is a quite popular attack. To avoid it, it is advisable to check the inward bound data, so the characters common towards SQL script will not likely come though. Probably the most visible ones are classified as the single and double quotes.

Source : CodeWebber