Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 984007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:56:48+00:00 2026-05-16T04:56:48+00:00

I am a new in CakePHP and I am looking for some code, I

  • 0

I am a new in CakePHP and I am looking for some code, I have been downloaded function which looks like below:

$this->set(array('fruit' => 'orange', 'vegetable' => 'kale'));

In the code, the array variables are accessed in another controller function using this method:

$varsSet = $this->viewVars;
echo $varsSet['vegetable'];

What I would like to do access the array variables in the same function in the controller where the $this-set() statement is made, and it seems like I should be able to do so with just one line of code. I have tried all of the following:

echo $fruit;
echo $this->field('fruit');
echo $this->MyModel->$fruit;
echo $this->MyModel->field('fruit');

And all of these throw parse, undefined variable, or variable not found errors. What would be the simplest/most proper way to access the variable within the same function in the controller?

Thanks,

Jonathan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:56:48+00:00Added an answer on May 16, 2026 at 4:56 am

    Function $this->set() in the controller is used to pass variables from the controller to the view.

    I.e.

    If you have:

    $this->set('fruit' => array('orange', 'vegetable' => 'kale'));
    

    Then in the associated view you can access the array directly as

    print_r($fruit);

    If you want to use fruit variable in the controller, then you have to assign it to the var i.e.:

    $fruits = array('orange', 'vegetable' => 'kale');
    $this->set('fruit', $fruits);
    

    But your question is not very clear what do you want to achieve with this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to cakephp and following this tutorial. http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html I have created the blog
I'm new to cakephp. I'm using the 2.0.5 release, according it looks like rails
I have a new CakePHP site, and I wish to redirect some old urls
So I'm new at this whole CakePHP thing, but I'm looking to get the
I'm using the new CakePHP 2.1 and would like to use the JsonView to
I'm extremely new to CakePHP. From what I've gathered, it seems like I can
I'm new to cakephp so go easy on me! I have a table called
I have a website made with CakePHP 1.3.7. This website has it's own login
I'm super new to CakePHP so I have a feeling that I'm probably just
I'm new to cakephp and I'm having some problems with setting up a local

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.