Showing posts with label CakePHP. Show all posts
Showing posts with label CakePHP. Show all posts

Thursday, 16 March 2017

CakePHP Interview Questions and Answers for fresher + experienced

Here we’ll discuss about some basic and advanced cakephp interview questions and answers which are most ask-able questions during interview session, These CakePHP questions and answers will help you to crack CakePHP technical interview round.

Question: What is Cakephp?

CakePHP is a free, open-source, MVC (Model, View, Controller) rapid development framework for PHP. It’s a framework which help you to make development fast and secure. CakePHP goal is to enable developers to work in a structured and rapid manner–without loss of flexibility.


Question: What is MVC in CakePHP?

Model view controller (MVC) is an architectural pattern used in software engineering.
Model: Handle database related functionality, manipulating database related query like add, edit , delete.
View: Design parts written here (HTML+PHP)
Controller: Business Logic goes here

Question: What Is The Name Of Cakephp Database Configuration File Name And Its Location?

FileName: database.php.default
Location: /app/config/database.php.default

Question: What is First cakphpe file loaded in application?

bootstrap.php


Question: What is the folder structure of CakePHP?

cakephp/
app/
Config/
Console/
Controller/
Lib/
  Locale/
  Model/
  Plugin/
  Test/
  tmp/
  Vendor/
  View/
  webroot/
    .htaccess
  index.php

 lib/
 plugins/
 vendors/
 .htaccess/
 index.php/
 README.md/

Question:What Is The Use Of Security.Salt And Security.CipherSeed In Cakephp?

The Security.salt is used for generating hashes.
The Security.cipherseed is used for encrypt/decrypt strings.

Question: What is default function for a controller?

function index() is default function in controller.


Question:Which function is executed before every action in the controller?

function beforeFilter()

Question: What Is Scaffolding In Cakephp?

Scaffolding is a technical way that allows a developer to define and create a basic application that can create, retrieve, update and delete objects.

Question: List some of the features in CakePHP?

* MVC architecture
* In-Built validations
* Caching
* Scaffolding
* CSRF protection via Security Component.
* Access Control Lists and Authentication and creating role management system.

Question: How to add Scaffolding in your application?

By adding $scaffold variable in the controller see following example.

<?php
class PostsController extends AppController {
    var $scaffold;
}
?>

Question: How To Include Components In Controller?


echo $this->here;

Question: How To Get Controller Name In CakePHP Views?

$this->request->params['controller']


Question: How To Get Controller Action Name In CakePHP Views?

$this->request->params['action']

Question: Which Methods Are Used To bind And Destroy Model Associations?

bindModel() : used to bind Model Associations
unbindModel() : used to destroy Model Associations

Question: What Is The Default Extension Of view files In Cakephp?

.ctp

Question: What is a Helper in CakePHP?

Helpers in CakePHP are associated with Presentation layers of application.Helpers mainly contain presentational logic which is available to share between many views, elements, or layouts

Question:What is a Behavior in CakePHP?

Behaviors in CakePHP are associated with Models.Behaviors are used to change the way models behaves and enforcing model to act as something else.

Question: What is the difference between Component, Helper, Behavior?

Component is a Controller extension, Helpers are View extensions, Behavior is a Model Extension.

Question: How to set variables for views?

$this->set('name','Cake PHP');

Question: How to set layout for controller

By adding $layout variable in the controller see following example.

<?php
class PostsController extends AppController {
    public $layout = 'layoutname';
}
?>

Setting layout for particular action,

layout = ‘layoutname’;
}
}
?>

Question: How to write, read and delete the Session in cakephp?

$this->Session->write(‘User.name’, ‘Rohit Kumar’);
$black = $this->Session->read(‘User.name’);
$this->Session->delete(‘User’);

Tuesday, 7 March 2017

How to upload file in cakephp without refersh page

Change image without refresh page in php and jquery, Upload file without refresh page in cakephp and jquery


Here we are going discuss about how to upload image or file without refresh page in cakephp and jquery.

Create controller in cakephp

ProfilesController.php

class ProfilesController extends AppController {
var $name = 'Profiles';
public $uses = array('Profile');
function changeprofilephoto() {
$profile_id = “14”; //
$path = "../../app/webroot/profilepic/";//set path
$valid_formats = array(".jpg", ".png", ".gif", ".bmp", ".jpeg");//
if($this->data)
{
$this->Profile->set( $this->data );
$name = $this->data["Profile"]['profile_pic']['name'];
$size = $this->data["Profile"]['profile_pic']['size'];
if(strlen($name))
{
$fileExt = substr(strrchr($name, '.'), 0);
if(in_array($fileExt,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = strtotime(date('Y-m-d H:i:s')).$fileExt;
$tmp = $this->data["Profile"]['profile_pic']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$this->Profile->set($this->data);
$this->Profile->id=$profile_id;
$this->Profile->saveField('uploadfoldername',$actual_image_name);
echo "<img src='/profilepic/".$actual_image_name."' class='preview'>";
$this->Session->write('suc','1');
$this->redirect($_SERVER['HTTP_REFERER']);
}
else
echo "failed";
}
else
echo "Image file size max 1 MB";
}
else
echo "Invalid file format..";
} else
echo "Please select image..!";
exit;
}
}
}


Create view file

Views/Profiles/changeprofilephoto.ctp

 <?php
echo $this->Html->script('jquery.min.js');
echo $this->Html->script('jquery.form.js');
?>

<script type="text/javascript" >
$(document).ready(function() {
$('#profile_pic').live('change', function(){
$("#preview").html('');
$("#preview").html('<img src="../img/loader.gif" alt="Uploading...."/>');//download loding image
$("#Profile").ajaxForm({
target: '#preview'
}).submit();
});
});
</script>
<style>
.preview
{
width:200px;
border:solid 1px #dedede;
padding:10px;
}
#preview
{
color:#cc0000;
font-size:12px
}
</style>
<div class="layout-popup f-left">
<?php
echo $this->Form->create('Profile',array('id'=>'Profile', 'controller'=>'Profiles','action'=>'changeprofilephoto', 'type'=>'file'));
?>


<!-- start id-form -->
<table class="frm-tbl" id="id-form" >
<tr>
<td colspan="2" class="align-t-r" >
<div class="f-left popup-title">Change profile image</div>
</td>
</tr>
<tr>
<td>Upload your image</td>
<td align="left" >
<!-- <input type="file" name="[Profile][photoimg]" id="profile_pic" /> -->
<?php echo $form->file('profile_pic', array('id'=>'profile_pic', "label" => false, "div"=>false, 'class'=>'styled-input-big'))?>
</td>
</tr>
<tr>
<td colspan="2">
<div id='preview'>
</div>
</td>
</tr>
</table>
<?php
echo $this->Form->end();
?>
</div>

Thanks cheers :)

Wednesday, 1 March 2017

How to create custom component in cakephp

How to create custom component in cakephp



In this tutorial we are going to write our own hello world component (plugin) in CakePHP.

The component is a logic container in CakePHP (ex. plugin in WordPress). All just needed is, include it and utilize those functions in that component. If some CakePHP developers want share their programming logic or functionality with fellow CakePHP developers, they will write as a component and share it with their programming community.

Once your Cakephp installation, database connections and security salt setting everything done. We are now able write our first hello world CakePHP component.


Step 1:

I am going to create component in the name of muni i.e. MuniComponent

To create your CakePHP component all you needed is to create php file in the follwoing directory. That’s app/controller/component/MuniComponent.php

Every component class we are creating must extends base component class that’s in the CakePHP library folder (lib/Cake/Controller/Component.php). So first in the component import component class.

App::uses('Component', 'Controller');

Where Controller – Indicates folder name
Component — Indicates class name we are including in the file.


Now create Component class, and write hello() function that returns ‘hello world’ string.

<?php 

App::uses('Component', 'Controller');

class MuniComponent extends Component {

    // hello world function
public function hello() {
return 'hello world';
}
}

?>

That’s it you successfully created your first hello world compnont in CakePHP, so any one use your component in their application .i.e. controller.

Step 2:


Now we are going to use the componenet(MuniComponent) just we written now. So I am going use this component in my UsersController. Before use any component in a controller, we must include it in a controller.

public $components = array('Muni');

Now I am going use hello() function of the component in the Controller, which going to return ‘hello world’ string.

<?php

App::uses('Controller', 'Controller');

class UsersController extends Controller{

//include component you want to use
public $components = array('Muni');

public function index()
{
$this->layout = 'common';
//calling component function we written
$data = $this->Muni->hello();
$this->set( 'data', $data);
}
}
?>


I have added one more function in the MuniComponent, that’s passwordGenerator() function which going to return random password when we are going to call it.

// password generator function
public function passwordGenerator( $length = 10){
    $letters ="@#$%^&*()_-+<>':,.1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    return substr(str_shuffle($letters), 0, $length);
}

In controller

//calling password generator function of component
$pass = $this->Muni->passwordGenerator(20);
$this->set( 'pass', $pass);

HTML APIs: What They Are And How To Design A Good One

As JavaScript developers, we regularly forget that not everybody has a similar data as USA. It’s referred to as the curse of knowledge:...