Showing posts with label cakephp. Show all posts
Showing posts with label cakephp. Show all posts

Code Generation with Cakephp - Creating Plugin


How to create Plugin in CakePhp using command line?


I assume you already have set up code generation with cakephp, if not see our post about `code generation with cakephp`. Using cmd prompt it quite simple.

Step 1: Type this command

cake bake plugin admin

Step 2: Create Model
 cake bake model plugin --admin (note: admin is plugin name in camelcase)

It will show list of available models, select you want to bake like user model, news model etc.

Note: For cakephp 3+ versions you need to use
cake bake model --plugin PluginName ModelName

Step 3: Create Controller
cake bake controller plugin -- admin

It will show list of available controllers, select you want to bake like user controller, news controller etc



Note: For cakephp 3+ versions you need to use 
cake bake controller --plugin PluginName ControllerName

Step 4: create finally view files for your plugin
cake bake view plugin --admin



Note: For cakephp 3+ versions you need to use 
cake bake template --plugin PluginName TableName

It will show list of available controllers for which you want to make view files, select and done!

Your plugin is ready now.

You can check your plugin at www.yoursite.com/pluginname/controller/action

Creating Static pages in Cakephp

How to create Static pages in cakephp?

Well its quite simple to create static pages in cakephp but what's the better approach? Ya sure there are two possible ways, as I think. The one is Just static content to a static page and other way is creating a simple ctp file and serving all contents with database(may be also used static with if else type) .  The second approach is better because its has only one ctp file and serving to all contents. So we go here.

Method 1: If you want to create content pages like about us, privacy policy which contents can be changed by an admin interface follow these steps-

Step1: Change pagesController

class PagesController extends AppController {
function beforeFilter() {
        $this->Auth->allow('content');//to allow to be visible for non-logged in users, if you are using login system
        parent::beforeFilter();
    }
    public function content($id = null, $layout = null, $theme=null) {
        if ($layout) $this->layout = $layout;//if you are using mulitple layouts and themes and want to change it dynamicaly
        if ($theme) $this->theme = $theme;
     
        $this->set('content', $this->Page->find('first', array('conditions' => array('Page.id' => $id))));
         $this->Page->id= $id;
         $this->set('title_for_layout', $this->Page->field('title'));
     
    }
}

Step 2: Add a table `pages` with fields you need like id, title, content, image, theme,layout etc.
Step 3: In View/Pages add content.ctp

  <div class="row innerPage">
    <div class="col-lg-12 col-md-12 col-sm-12">
      <div class="row userInfo">
        <div class="col-xs-12 col-sm-12">
          <h1 class=" text-left border-title"> <?php echo $content['Page']['title'];?> </h1>
          <div class="w100 clearfix">
          <?php echo $content['Page']['content'];?>
          </div>
        </div>
      </div>
   </div>
</div>

However you can change html according to your need, I prefer bootstrap framework.

Then you can use it in your view file  as

 <?php echo $this->html->link("Terms of Services", array("controller" => "pages", "action" => "content", 5), array("class" => 'themeprimary','target'=>'_blank')) ?>

This will generate a link yoursite/pages/content/5. 5 is the id of row you want to show the details of.

If you want your link like yoursite/terms then you need one more step to go. In routes.php  add this line

Router::connect('/terms', array('controller' => 'pages', 'action' => 'content',5));

In case you want to change your theme/layout dynamically for different pages like contact us, about us etc pass layout and theme name in connect

Router::connect('/terms', array('controller' => 'pages', 'action' => 'content',5, 'your_layout_name','your_theme_name'));

Now change your link code

<?php echo $this->html->link("Terms of Services", array("controller" => "", "action" => "terms"), array("class" => 'themeprimary','target'=>'_blank')) ?>

Here routes.php will automatically manages the requests to correct controller and action.


Method 2: You simply need to display content without any database

Step1 : Just create a about.ctp under View/Pages and put the content you want to display
Step 2: Change your pagesController. add a method about

public function about($layout = null) {
        $this->set('title_for_layout', 'About');
     
    }

Thats it.

Code generation with CakePHP

How to set up console to generate code with cakephp?

Cakephp is a powerfull php framework that can generate working codes for you within minutes. To generate code cakephp uses console. There are step by step tutorial about how to set up all.

Step:1- Open command prompt using (window+R) key
Sttep:2- Check if its working already, tpye (cake) in console window. If it shows something like

'cake' is not recognized as an internal or external command
then you have to set up path environment variable.

Step:3 - You need to supply php.exe path and console script path which resides in lib\cake\console
So right click on my computer->properties->advanced system settings->Environment Variables->System variables->path->edit. Then in variable value you will see several values separated with (;) sign, you have to put your path here.
Just make a ; in last if not already there and paste your path here

If your xampp or wamp is under d:\xampp then

D:\xampp\php;

and if your cakephp folder name is cake then

D:\xampp\htdocs\cake\lib\Cake\Console;

putting it all together looks like

D:\xampp\php;D:\xampp\htdocs\cake\lib\Cake\Console;

Now its all done.

Step 4:- check its working or not by typing previous command (cake)

now it should produce result like this

Welcome to CakePHP v2.5.4 Console

Now you can use cake to bake your code, model, view, controller or projects

For baking a project navigate to folder where you want to create project and type

cake bake project myapp

this will successfully create your skelton project.