Jump to content

Cake: passing values to a function


RIRedinPA

Recommended Posts

I'm new to Cake (and using frameworks in general). I think I am having a syntax issue here. What I am trying to do is pass a value back to the function and then load a page based on the value passed:

 

Here's my controller:

 

<?php

    class BlogpostsController extends AppController { 
        var $name = "Blogposts";
        var $scaffold;
        
        function index() {
            $this->set('posts',$this->Blogpost->find('all'));
        }
        
        function viewuser($userid) { 
            $this->set('thisuser', $this->User->find($userid));
        }
    }
?>

 

In my cpt file I have this:

 

<td class="user"><?=$html->link($post['User']['name'], 'viewuser');?></td>

 

I know this won't work because I am not passing anything to viewuser, I get this error message:

 

Warning (2) <javascript:void(0);> : Missing argument 1 for viewuser() [APP/controllers/blogposts_controller.php, line 13]

 

Which is good, in a way. But if I rewrite it like this:

 

<td class="user"><?=$html->link($post['User']['name'], 'viewuser("1")');?></td>

 

I get another error:

 

Missing Method in BlogpostsController

Error: The action viewuser("1") is not defined in controller BlogpostsController

 

Which seems to be telling me I am not writing the syntax for passing values correctly...

Link to comment
https://forums.phpfreaks.com/topic/207273-cake-passing-values-to-a-function/
Share on other sites

This is happening because you probably haven't called the class as an object.

 

You'll need to call the class as an object first.

$bpControl = new BlogpostsController();

 

and then call the function within the class by doing something like this:

 

$userGet = $bpControl->viewuser("1");

 

That should solve the "method" error and all that. If you want to understand more about Objects, Classes and all that I provided a link, but here it is again: http://php.net/manual/en/language.oop5.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.