Posts

Showing posts with the label CakePHP

Multiple Images Upload in CakePHP

Image
In this post we will see how to uload multiple images in cakephp.I am going to show the additional code for multiple image handling for both the action Add and Edit.What all you need is to simply copy paste in your application according to needs. My table : posts CREATE TABLE `posts` ( `id` int(10) NOT NULL AUTO_INCREMENT, `title` varchar(200) NOT NULL, `body` text NOT NULL, `image1` varchar(200),  `image2` varchar(200),  `image3` varchar(200), PRIMARY KEY (`id`) ) As you can see what are the fields of my 'posts' table.I have three fields for the images.Example: image1,image2,image3 etc.You may have different fields. Add the code given below for multiple image handling. My uploaded images are stored in  app/webroot/img/uploads/posts  folder.I have one default image called 'no-icon.jpg' in the same folder which will be shown if the correct image is not available or in case of NULL value. Controller : PostsController.php Add Function public function add() { $this-...

Recursive in CakePHP

Image
Today we are going to learn about Recursive in CakePHP . Using this recursive property, Cake will know about the depth of the result that needs to be generated when find() and read() methods are used.It is needed to set the depth of the retrieval of records associated with a model data so we can limit how much data is fetched from the query in case of multi levels of associations between your models. Lets say for example we have Author module which has many Books.Now you have such case in which you want to retrive the data of the authors plus their books too.So in this case how Recursive play its role? We have Model file for Author having the following code. <?php class Author extends AppModel {    var $name = 'Author'; } ?> and Model for the Book, <?php class Book extends AppModel {    var $name = 'Book'; } ?> Now write the index function in AuthorsController.php file   public function index()    {       $authors = $thi...

Dynamic Fields Validation in CakePHP

Image
In this post we'll learn about dynamic fields validation .Generally validation is achieved by placing the validation rules in the model file.You can get the idea of how validation rules are written in model file by the example given below. <!-- file:app/models/yourmodel.php --> var $validate = array( 'name' => array( 'notempty' => array( 'rule' => array('notempty'), 'message' => 'This field is required..!', //'allowEmpty' => false, //'required' => false, //'last' => false, //'on' => 'create', ), ), 'SRRP' => array( 'numeric' => array( 'rule' => array('numeric'), 'message' => 'This field is required..!', //'on' => 'create', ), ) ); But here,the fields (Example: name,SRRP) are static fields.Following lines of code were written in view ...

CRUD in CakePHP Part : 2

Image
In previous post we learnt about view and listing of the Users.In this post we'll learn about add,edit and delete operations. Adding Users To add a user record put the below function in our UsersController.php public function add() {         if ($this->request->is('post')) {             $this->User->create();             if ($this->User->save($this->request->data)) {                 $this->Session->setFlash(__('User has been saved.'));                 $this->redirect(array('action' => 'index'));             } else {                 $this->Session->setFlash(__('Unable to add User.'));             }         }     } Here we have to include the SessionComponent ...

CRUD in CakePHP Part : 1

Image
In this post we will see how to create simple CRUD application in CakePHP. Creating the Users Database Let’s set up the database for our blog.Right now, we’ll create a single table for our users. We will  add few records of users in order to test. Simply run the SQL query given below into your DB: /* Create our users table: */ CREATE TABLE users (     id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,     firstname VARCHAR(50),     lastname VARCHAR(50),     email VARCHAR(50),     message TEXT ); /* Then insert some users for testing: */ INSERT INTO users (firstname,lastname,email,message)     VALUES ('Ricky', 'Astley', 'test1@test.com','Hello...!'); INSERT INTO users (firstname,lastname,email,message)     VALUES ('John', 'clinton','test2@test.com','How are You...!' ); INSERT INTO users (firstname,lastname,email,message)     VALUES ('Smith', 'Johnson', 'test3@test.com','I am Fine...!'); Create a Us...

CakePHP: URL Rewriting

Image
What is Mod_Rewrite? Simply put, mod_rewrite is an Apache module that let’s you rewrite urls based on rules you define. Other common uses of mod_rewrite:  • All traffic from multiple domain names can be directed to one domain. • Traffic from specific websites can be blocked. • You can block spammy searchbots and offline browsers from spidering your site and eating your bandwidth. • Mask file extensions can be done. • Image hotlinking (means ,other web pages that links to images that resides on your server) can be Prevented.  By using the overwrite rule and modifying the htacess file of the root folder CakePHP URLs can be made SEO friendly as shown below. This is an example of URL Rewrites for the site that has n level menus,  <IfModule mod_rewrite.c>    RewriteEngine on    RewriteRule ^([^/]+html)$ app/webroot/controller_name/function_name/$1 [L]    RewriteRule ^[^/]+/([^/]+html)$ app/webroot/controller_name/function_name/$1 [L] ...

URL rewriting is not properly configured on your server.

Image
You are reading this post that means you are facing the error of “ URL rewriting". Without solving this error ,you cant go ahead. To solve this issue you need to make some changes in the httpd.conf file of the apache server. If you are using wamp server,you can access the httpd.conf file as bellow. If you are using XAMPP server then, you can found httpd.conf file in the xampp/apache/conf/httpd.conf.For any other apache based server config file can be found in“apache/conf/httpd.conf”. Once you open the config file,you can find a line as “#LoadModule rewrite_module modules/mod_rewrite.so” .See the image bellow. Just remove the “#” in that line in the httpd.conf file. then save & restart the Apache Server. The problem must be solved. If not then make sure that the code written in apache/conf/extra/httpd-vhosts.conf file is like as the code given below. <VirtualHost *:80>     ServerAdmin admin@localhost     DocumentRoot "c:/wamp/www/"     Server...

How to install CakePHP on localhost Wamp

Image
In this post we will learn how to install & configure the CakePHP on localhost Wamp. I am using  Wamp Server as the web server but, it will remain same in XAMPP & many other Apache Based web servers as well because all those are build based on apache core. So, the configurations are mostly same as Wamp Server. So what you need to do is just follow the steps given below in order to install CakePHP on localhost. Step 1 : Go to CakePHP Official Website and Download updated Cake PHP and unzip the file. Step 2 :  Copy the downloaded folder in wamp folder : C->www->wamp->(cakefolder) Step 3 : Now Go to (cakefolder)->app->Config->database.php.defult and rename it as database.php.Now you need to make required changes in it.After the changes, the configuration setting look like bellow. class DATABASE_CONFIG {     public $default = array(         'datasource' => 'Database/Mysql',         'persistent' =...

MVC architecture of CakePHP

Image
CakePHP follows the MVC software design pattern. MVC Programming pattern will separate the application into three parts: - Model layer - View layer - Controller layer The Model layer The Model layer represents one part of the application that implements the business logic. Models are representations of database tables. They can connect to database, query it and save data to the database. In order to work with MVC its very important to note that there must be no interaction between models and views.All the logic will be handled by controllers. It’s responsible for fetching data and then after converting them into meaningful concepts for the application by processing,validating, associating or other similar tasks related to handling data. MVC Request The View layer You can describe Views as template files that present their content to the user.variables, arrays and objects that are used in views are registered through a controller.A presentation of the modeled data will be r...

Introduction to CakePHP

Image
CakePHP is a rapid development framework for PHP that provides an extensible architecture for developing, deploying, and maintaining applications. It uses MVC software design pattern.The CakePHP framework also provides valuable reusable libraries for dealing with common tasks. Using CakePHP’s scaffolding feature, it’s possible to build a prototype application very quickly, using a less amount of code and with a large number of helper classes available to extend and customize your application.CakePHP is being actively developed, and is backed by extensive documentation and has a lively support community. Features of CakePHP Although CakePHP Development has multiple features supporting it to be chosen as the most preferable framework for developing complex web applications, we put in highlights a few convincing ones.      (1)  It is Compatible with both PHP4 and PHP5.     (2)  It provides DBMS support for rapid and bendable templating.     (3)...