Welcome Guest, Not a member yet? Register   Sign In
How can this not work??? help!
#21

[eluser]Randy Casburn[/eluser]
To be sure, both of these guys know exactly what they are talking about. So... All I've done is give you exactly what they have said in code that will work doing things the way you apparently prefer. I've done nothing different here...

Here's the controller...
Code:
class Welcome extends Controller {
    
    function Welcome()
    {
            parent::Controller();
            $this->load->library('menumaker');
    }
    
    function index()
    {
            $data['my_anchor'] = $this->menumaker->make_menu('fred','fred');
            $this->load->view('welcome', $data);
        }

Here's your class...

Code:
class Menumaker {

    var $obj;

    function Menumaker()
    {
        $this->obj =& get_instance();
        $this->obj->load->helper('url');
    }

    function make_menu($thing, $thing2)
    {
        return anchor($thing, $thing2);
    }
}

Place your link your view with <?=$my_anchor?>

This works flawlessly,

Hope this is helpful,

Randy
#22

[eluser]Pascal Kriete[/eluser]
[quote author="Luke D." date="1220334392"]
My old development pattern, which might be wrong for Codeigniter projects, has been to write my classes and then run them as a script for de-bugging. Then once the class works I stick it into the project. So, I wrote this class the same way - with the file in the application library folder, which would be its final location, and I ran it from there - basically previewing the page in Safari on my test server(MAMP). And when I do that it is a blank screen and blank source code - total fatal error just as if I left off a brace or a ;. The problem is definitely in the loading of the url helper.
[/quote]
To quote Shakespeare:
Quote:Ay, there's the rub.
When you run CodeIgniter normally, the so called 'super-object' is created. This is essentially an instance of the Controller (which extends the base Controller, which in turn extends CI_Base). This object also contains pointers to instances of a lot of the core libraries, such as the loader, output, benchmark, and uri class.

By the very nature of how it's set up, the controller inherits all of this, so all those objects are available through $this. Models, and views work a little differently, but they also have access to these resources.

Libraries and helpers are not connected to the super-object in any way, so they do not have access to any of these resources. That's what get_instance() is for. It returns the current instance of the super object (a singleton design pattern).

When you run the class yourself, none of this happens. In fact, the Base4/Base5 file (which contain the get_instance function) doesn't even get included. And even if it did, there is no lazy instantiation so it wouldn't help.

You could manually include the url helper (it's found in system/helpers/url_helper) - after all, they are only collections of functions. In this case however, the url helper relies on the config class, so you're stuck.

Long story short - you cannot run most helpers or libraries by themselves.

[EDIT: And just as an add on: you could just call the class through a very basic controller - one that you know doesn't generate any errors, that would give you a rather well isolated test case. Also, you should turn display_errors on in your php.ini, it makes debugging a lot easier. The xdebug pecl extension is quite helpful as well.]
#23

[eluser]Randy Casburn[/eluser]
@inparo you are so correct...

To quote Shakespeare:
Quote:Ay, there's the rub.

@luke - To be sure...

Quote:If I run the class from a controller and instantiate the url_helper from the controller, then the class works fine, but this strikes me as really violating the thinking behind writing classes - a class should work on its own (or at least it strikes me that it should work this way.

There isn't a thing wrong with your approach as long as you're going to study the API you're involving first and understand all the inter-dependencies that exist. Yes, a class should stand on its own. But if you pull in features of other classes you are obligated to abide by the dependencies of those classes as well. That's what you've missed here.

That said, you've being much too academic in your approach and that's why you stumbled here. You've created dependencies by using CI super object methods and you didn't recognize or don't understand you were doing this. Obviously you missed the nature of the CI super object and how it is constructed (what inparo discussed above) when you researched the CI code base.

If you're going to take this approach, I recommend thoroughly understanding what the codeigniter.php file does and reading through the code in the libraries folder while constructing your next class.

Randy
#24

[eluser]Luke D.[/eluser]
Hi All,

Hey Randy, that code snippet you sent worked dandy. Thanks! Now I have new problems!

Luke
#25

[eluser]Randy Casburn[/eluser]
Right...knew that already :lol: The real question can you explain why?

Have fun.

Randy
#26

[eluser]Luke D.[/eluser]
Yep. Didn't get access to the CI Superobject correctly. Codeigniter classes must be developed in the Codeigniter framework (call it an "environment"). Yes a class can stand alone as a class, but it has to stand alone WITHIN Codeigniter, which makes a lot of sense actually - if you are going to use a piece of Codeigniter to make something it is fair that one abides by the Codeigniter rules.

I am a big believer in diving right it, so I've been building things in CI and at the same time reading stuff. I even bought some book. The manual, though, is pretty darn good. Most of it is I have to develop a workflow or at least an approach to how I do things in CI.

L
#27

[eluser]Randy Casburn[/eluser]
You've got a great start...you're doing great. Keep pressing on and...really...I mean it..just keep having fun with this stuff ;-)

Take care,

Randy
#28

[eluser]Luke D.[/eluser]
Randy,

Thanks for the encouragement and especially for the all help.

Luke




Theme © iAndrew 2016 - Forum software by © MyBB