1

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 ?.

  • Possible duplicate: http://stackoverflow.com/questions/30318793/how-should-i-use-requestaction-in-the-view-with-cakephp-3-x – Inigo Flores Jan 19 '16 at 18:46

3 Answers3

2

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

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);
Agilanbu
  • 2,747
  • 2
  • 28
  • 33
0

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);
Agilanbu
  • 2,747
  • 2
  • 28
  • 33