Question

I am using cakePHP 1.26.
The web page turned out blank when I tried to use requestAction to access a function in a COntroller from a .ctp.
Here is the code:

<?php
class TestingController extends AppController {

 function hello($id=null){

           $IfLoggedIn=$this->Session->check('user');

           if($IfLoggedIn){
            //search the database
            //$result=doing something from the search results
            $this->set('userInfo',$result);
            return "2";
           }

           else if(!$IfLoggedIn && $id!=null){
           return "1";
            }

           else if($id==null){
           return "0";
            }
    }
}

and then in default.ctp file, I made use of the function defined above:

<?php
    $u = $this->requestAction('/hello'); 
    if($u=="2"){
    echo "welcome back, my friend";
    }
    else{
    echo "Hello World";
    }
?>

But when I load a web page, it was blank page.

I have no idea what's wrong in the code.

Was it helpful?

Solution

Try to add

$u = $this->requestAction('/hello', array('return'=>true));

Check this

OTHER TIPS

You might try including the controller in the url param of requestAction.

If you spend more time debugging and reading the manual, you'll learn more, more quickly.

I'm new to Cakephp myself, I'm using 2.0 which may be different in your version of Cake.

I found that the following code from the manual was wrong for me:

<?php
class PostsController extends AppController {
    // ...
    function index() {
        $posts = $this->paginate();
        if ($this->request->is('requested')) {
            return $posts;
        } else {
            $this->set('posts', $posts);
        }
    }
}

You need a slight modification (seems the manual was wrong in this case). The following code worked for me:

<?php
class PostsController extends AppController {
    // ...
    function index() {
        $posts = $this->paginate();
        if ( !empty($this->request->params['requested'])  )  { // line here is different
            return $posts;
        } else {
            $this->set('posts', $posts);
        }
    }
}

We shouldn't be checking for the request HTTP verb, we should be checking if the request parameter is true.

Here's another relevant link to the manual about request parameters: http://book.cakephp.org/2.0/en/controllers/request-response.html#accessing-request-parameters

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top