简体   繁体   中英

How to call function from controller in different view in cakephp 3?

I have function in ProductsController productsCount() . It give me amount of records in table.

    public function productsCount() {
        $productsAmount = $this->Products->find('all')->count();

        $this->set(compact('productsAmount'));
        $this->set('_serialize', ['productsAmount']);

    }

I want to call this function in view of PageController. I want to simply show number of products in ctp file.

How can i do this?

You can use a view cell . These act as mini controllers that can be called into any view, regardless of controller.

Create src/View/Cell/productsCountCell.php and a template in src/Template/Cell/ProductsCount/display.ctp

In your src/View/Cell/productsCountCell.php

namespace App\View\Cell;

use Cake\View\Cell;

class productsCountCell extends Cell
{

    public function display()
    {
        $this->loadModel('Products');
        $productsAmount = $this->Products->find('all')->count();

        $this->set(compact('productsAmount'));
        $this->set('_serialize', ['productsAmount']);
    }

}

In src/Template/Cell/ProductsCount/display.ctp lay it out how you want:

<div class="notification-icon">
    There are <?= $productsAmount ?> products.
</div>

Now you can call the cell into any view like so:

$cell = $this->cell('productsCount');

I think it would make more sense to just find the product count in the PageController. So add something like $productsAmount = $this->Page->Products->find('all')->count(); in the view action of PageController, and set $productsAmount . If Page and Products aren't related, then you can keep the find call as is as long as you include a use for Products.

Also check this out for model naming conventions: http://book.cakephp.org/3.0/en/intro/conventions.html#model-and-database-conventions

Model names should be singular, so change Products to Product.

you can not call controller method from view page. you can create helper, which you can call from view page.

here you will get a proper documentation to creating helpers- http://book.cakephp.org/3.0/en/views/helpers.html#creating-helpers

It just depend on the kind of call you're making because there are 3 cases for your issue.. 1- If you're calling by a link to click you simply do:

<?= $this->Html->link(_('Product number'),['controller' =>'ProductsController', 'action' => 'productsCount']) ?>

The 2 other cases are whether you want to render the result straight in that same view, then there are some workaround to do. 1- first you will need to check what are the associations between the Page table and the product table and use BelongTo or hasMany option to bind them togheter for proper use. 2- If no association between the tables then you will nedd TableRegistry::get('Produts'); to pass data from a model to another, just like this way in the Pages controller:

 public function initialize()
        {
            parent::initialize();
$this->Products = TableRegistry::get('Produts');
}

But i quite believe that the first option is more likely what you described.

Also you can define static method as below

public static function productsCount() {
    return = $this->Products->find('all')->count();
}

And use self::productsCount() in other action.

This is useful only if you need to get count multiple time in controller. otherwise you can use it directly in action as below:

$this->Products->find('all')->count();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM