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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.