How to access a controller action , from ctp file in CakePHP 3

1.5k views Asked by At

I want to access a controller action from ctp file. Here my action name and ctp file name is different. For example I created an action in the name of tickets ,and my view ctp file name is ticket_title. How to do this ?

This is my action : ticket

class UsersController extends AppController
{
public function ticket()
{
 $ticket=$this->Tickets->find('all');
 $this->set(compact('ticket'));
}
}

My view ctp file : ticket_title.ctp

<?php
$this->requestAction(array('controller' => 'users', 'action' => 'ticket'));

foreach($ticket as $ticket1)
{
    echo $ticket1->title."<br/>";
}

Can any one help me ?.

3

There are 3 answers

0
Balasuresh Asaithambi On

Finally , it works by using render() method. In my action:

public function ticket()
{
$this->loadModel('Tickets');
$ticket=$this->Tickets->find('all');
$this->set(compact('ticket'));
$this->render('ticket_title');
}

And this is my ticket_title.ctp

<?php
foreach($ticket as $ticket1)
{
    echo $ticket1->title."<br/>";
}
0
Mohit saini On

you can use this object in any other controller or in ctp file where needed

(in case of controller)

use App\Controller\ControllerName;

$ControllerNameObj = new ControllerName;

$ControllerNameObj->functionName();

(in case of ctp file)

$abcObj = new \App\Controller\HomeController; 

$fetchdetail = $abcObj->ControllerfunctionName($parameter1, $parameter2);
0
Gaurav Singhal On

Yes, its working in CTP of Cake PHP 3 . You can use this object in any other controller or in ctp file where needed

(in case of controller)

use App\Controller\ControllerName;

$ControllerNameObj = new ControllerName;

$ControllerNameObj->functionName();

(in case of ctp file)

$abcObj = new \App\Controller\HomeController;

$fetchdetail = $abcObj->ControllerfunctionName($parameter1, $parameter2);