Welcome Guest, Not a member yet? Register   Sign In
Change controller class property
#1

[eluser]yabune[/eluser]
Hi,

I define a property inside a controller class:
Code:
class Xpto extends Controller {
  private $abc;

And I can set the value inside the constructer:
Code:
function Xpto()
{
  parent::Controller();
  $this->abc = 'some value';
}

But If I change the value inside another function, the variable doesn't change...

I think it's because when I call a function, the object class is recreated... Is there any way to store the variable value? How can I keep its value?

Any help on this?

Thanks!
#2

[eluser]jedd[/eluser]
[quote author="yabune" date="1258921678"]

I define a property inside a controller class:
Code:
class Xpto extends Controller {
  private $abc;

And I can set the value inside the constructer:
Code:
function Pages()
{
  parent::Controller();
  $this->abc = 'some value';
}
[/quote]

Is Pages really the constructor for your Xpto class?

That's neat.
#3

[eluser]yabune[/eluser]
No, it's just an example... Smile
I have it right in my CI application.
#4

[eluser]jedd[/eluser]
[quote author="yabune" date="1258925127"]No, it's just an example... Smile
I have it right in my CI application.[/quote]

Posting code that isn't causing you problems, but has its own glaring bugs, is probably not as helpful as you might think.

Can you post your example code - ideally an entire controller that demonstrates this bug - so we can help identify your actual problem?
#5

[eluser]yabune[/eluser]
Sorry, I just wanted to simplify but you're probably right, here it's the actual code.

Code:
<?php

class Pages extends MY_Admin_Controller {

    private $abc;

    function Pages()
    {
        parent::MY_Admin_Controller();
        $this->abc = 'bbb';
    }
    
    function index()
    {
    }
    
    function show($page, $sucess = 0)
    {
        echo 'abc:'.$this->abc;
    }
    
    function update($page)
    {
        $this->load->helper('url');
        $this->abc = 'xxxxx';
        redirect('/admin/pages/show/'.$page.'/1', 'location');
    }
}
/* End of file pages.php */

If I call the show function I get bbb.
And if I call the update function I still get bbb and not xxxxx.

Is it because when I redirect a new Pages object is created?
How do I pass a string to another function?

I know I could just call inside update function: $this->show(with some args)
But I would like that the url wasn't http:/.../update but instead to be http://.../show.

Thanks!
#6

[eluser]jedd[/eluser]
[quote author="yabune" date="1258930217"]
If I call the show function I get bbb.
And if I call the update function I still get bbb and not xxxxx.

Is it because when I redirect a new Pages object is created?
[/quote]

Well, yes. It's actually a whole new program instance that is run. It might have a sense of nostalgia for previous activity, but it certainly won't remember the variable that was instantiated the last time it was run.

As evinced by making a change like this to your example code:
Code:
function update($page)
    {
        $this->load->helper('url');
        $this->abc = 'xxxxx';
        $this->show();
        // redirect('/pages/show/'.$page.'/1', 'location');

Quote:How do I pass a string to another function?

In the URL, generally, is the easiest and best place (for varying values of best). Alternatively in POST data, or Session data. But I agree with you about keeping as much data as possible in the URL - usually a good thing.
#7

[eluser]yabune[/eluser]
What I would like to pass to the show function is any error I would get in uploading a file, so that I can show a div displaying the error.

Is it good practice to pass an error description with the url? Isn't too much information?

If I don't redirect, and call the show function as you mentioned, can I change the url in the browser to http://.../show?
#8

[eluser]jedd[/eluser]
[quote author="yabune" date="1258931912"]
What I would like to pass to the show function is any error I would get in uploading a file, so that I can show a div displaying the error.
[/quote]

Ahh, okay. What most people do here is set some [url="/user_guide/libraries/sessions.html"]flash data[/url].

I'm in two minds about flash data. It's very handy, certainly, but the occasional redirect here and there, having to track that and do the odd keep_flashdata() call, and the fact you can't seem to test for its presence on the same run that you set it (I think?), often makes it a bit frustrating. Instead I tend to just stick with normal session data, and just remove (unset) it when I use it.

Anyhoo, in the place that you generate the error - pop some session data in with whatever you need to know later. Later, when you come into your page, check for the presence of that session data, generate whatever view partial with the <div> stuff you need, feed this into your main view, and unset the session data.

Easy.

Quote:Is it good practice to pass an error description with the url? Isn't too much information?

No, you're right - this kind of ephemeral data should not be in the URL.

Quote:If I don't redirect, and call the show function as you mentioned, can I change the url in the browser to http://.../show?

Changing URL's from within a page is not possible (exception being javascript's ability to change anything after the #) and for good reason - huge security hole if you could do that.
#9

[eluser]yabune[/eluser]
Thanks a lot!

Now I have some very good lights in where to go... Smile

I'll check both flash and regular session data.




Theme © iAndrew 2016 - Forum software by © MyBB