Coding Cheatsheets - Learn web development code and tutorials for Software developers which will helps you in project. Get help on JavaScript, PHP, XML, and more.

Post Page Advertisement [Top]

CakePHP testimonials

Components and Plugins are still separate entities in Cake 2.0. According to the manual components are "are packages of logic that are shared between controllers", whereas plugins are "a combination of controllers, models, and views". Compoments extend the base Component class, while Plugins have their own AppModel and AppController.
Think of a plugin as a separate Cake application sharing the same core libraries with your main application.
How to use vendor in cakephp
App::import('Vendor', 'Custom/getdata');
$obj = new GetData();

Lib

Contains 1st party libraries that do not come from 3rd parties or external vendors. This allows you to separate your organization’s internal libraries from vendor libraries.

Vendor

Any third-party classes or libraries should be placed here. Doing so makes them easy to access using the App::import(‘vendor’, ‘name’) function. Keen observers will note that this seems redundant, as there is also a vendors folder at the top level of our directory structure. We’ll get into the differences between the two when we discuss managing multiple applications and more complex system setups.
To clarify further, Libis recommended for libraries that you write yourself. This may just be a few classes or entire libraries. Vendor is recommended for libraries or scripts that you may download from github for instance. Plugin is strictly for cakephp framework plugins.

Regarding Lib vs Vendor for you own scripts or 3rd party scripts there is no difference that I am aware of. I have put my own scripts in both as well as 3rd party scripts in both locations and it hasn't made any difference. It's just a recommended way to organize your files.
You can load your scripts from Lib or Vendor using App::import() which is the same as require_once(). 
To load framework files or your own scripts that follow cakephp conventions, you would use App::uses().
Examples: How to use helper function inside controllers
    App::uses('CommonHelper', 'View/Helper');
    $yourHelper = new CommonHelper(new View());
    pr($yourHelper->getBrowser()); exit;
                OR
    App::import('View/Helper', 'CommonHelper');
    $yourHelper = new CommonHelper(new View());
    pr($yourHelper->getBrowser()); exit;
Examples: How to use element inside controllers
    $view = new View($this, false);
    $content = $view->element('header', $params);
                OR
    $this->render('/Elements/header');
                OR
    $this->render(null, 'ajax', VIEW . /elements/header.ctp);
Examples : Extend view inside another view.
    // app/View/Common/index.ctp
    <h1><?php echo $this->fetch('title'); ?></h1>
    <?php echo $this->fetch('top-content');
        echo $this->fetch('panels'); ?>
    <?php
    // app/View/Locations/view.ctp
    $this->extend('/Common/index');
    $this->assign('title', 'Some Title');
    $this->start('top-content');
    ?>
    <div id="map_canvas">
      <p>Hello, world!</p>
    </div>
    <?php $this->end();
    $this->append('panels');
    echo '<div class="box-title">
                <i class="icon-list"></i>
                Publishing
            </div>'.
        $this->Form->button(__d('croogo', 'Save')),
        $this->Html->link(__d('croogo', 'Cancel'),
            array('action' => 'index'),
            array('button' => 'danger')
        );
    $this->end();?>
Examples: How to create breadcrumb
    Using the HTML Helper:
    echo $this->Html->getCrumbs(' > ','Home');
    Note: $this->Html->getCrumbs(): It gets all the addCrumbs being added. The parameters are the “separator” ( >) which can be replaced by anything of our wish.
    $this->Html->addCrumb('Users', '/users');
    $this->Html->addCrumb('Add User', '/users/add', array('class' => 'breadcrumblast'));
    Note: $this->Html->addCrumb() : Add breadcrumb element
    Output:
    Home > Users > Add User

No comments:

Post a Comment

Bottom Ad [Post Page]