user1496047
user1496047

Reputation: 51

Cakephp 2.0 + Elements + requestAction = empty result

I'm trying to use Elements in Cakephp 2.0 without luck. I have a model named Post, a controller named Posts and various views. In the layout I would like to include (for every page/view) a box with the most recent news (say 2).

So I created the element dok-posts.ctp

<?php $posts = $this->requestAction('posts/recentnews'); ?>
<?php foreach($posts as $post): ?>


<div class="Post">
....

In my PostsController I added the function recentnews()

public function recentnews(){
$posts =  $this->Post->find('all',array('order' => 'Post.created DESC','limit' => 2));
if ($this->request->is('requested')) {
    return $posts;
} else {
    $this->set('posts', $posts);
    }
}

In my layout, default.ctp I call my element

<?php echo $this->element('dok-posts'); ?>

The problem is that I get this message

Invalid argument supplied for foreach() [APP\View\Elements\dok-posts.ctp, line 9]

Debugging in dok-posts.php, right after the $this->requestAction, gives me an empty line. It seems that the recentnews function is not returning anything (debugging in the function returns an array with the posts found). Can anyone please tell me what am I doing wrong?

Upvotes: 3

Views: 5692

Answers (5)

Anubhav
Anubhav

Reputation: 1623

In your app controller create beforefilter and getNewsElement function.

public function beforeFilter() {
    parent::beforeFilter();
    $data = $this->getNewsElement();
    $this->set('posts',$data);
}

function getNewsElement(){
# Put Post model in uses
$posts =  $this->Post->find('all',array('order' => 'Post.created DESC','limit' => 2));
return $posts;
}

dok-posts.ctp
#Remove <?php $posts = $this->requestAction('posts/recentnews'); ?>
<?php foreach($posts as $post): ?>

<div class="Post">
....

In  PostsController
public function recentnews(){
$posts =  $this->getNewsElement();

    $this->set('posts', $posts);

}

This will solve your problem!

Upvotes: 1

sujit singh
sujit singh

Reputation: 1

Try this

 <?php $this->requestAction('/controllerName/functionName');?>

Upvotes: 0

user1496047
user1496047

Reputation: 51

I have followed the example in the Cakephp's guide. From the moment it does not passes the if statement it seems that there is a problem with the control.

$this->request->is('requested')

So I removed the

is->('requested') 

adding

->params['requested']

and it works.

Thank you all for your help (especially wnstnsmth for the solution).

Upvotes: 0

grssnbchr
grssnbchr

Reputation: 2974

Since you found out that the action is actually called,

$posts = $this->requestAction('posts/recentnews');

is working correctly. Here, for clarity and extended configuration options (for later changes to the code), I suggest you to use a Router array instead of an URL

$posts = $this -> requestAction(array(
 'controller' => 'posts',
 'action' => 'recentnews'
));

Now to your actual problem... Since you say, it always goes into the else branch,

$this->request->is('requested')

might not work as expected. Try this (it works perfect for me):

if (!empty($this -> request -> params['requested'])) {
   return $posts;
}

Upvotes: 3

RichardAtHome
RichardAtHome

Reputation: 4313

Try

<?php $posts = $this->requestAction('/posts/recentnews'); ?>

(note the leading forward slash)

Upvotes: 0

Related Questions