user3108967
user3108967

Reputation: 43

Links, Controllers in CodeIgniter

I'm starting with CI and I need some help. I'm trying to load some html pages with Ajax, this html files are stored in view folder, and I'm trying to acess this files using a controller and I have no success until now. I want to know, how I can acess this files and if the controller that i'm using it's correct or there is way better to do it.

Controller

class Router extends CI_Controller
{
     public function index($file)
     {
           $this->load->view($file);
     }
}

Ajax

var SampleFunction = function (router) {//router is my base_url() + '/router'
    var pageContentBody = $('.page-content .page-content-body');

    if ($("#startLoadTag")){
        $.ajax({
            type: "post",
            cache: false,
            url: router + '/SampleLink.html',
            dataType: "html",
            success: function (html) {
                pageContentBody.html(html);
            }
        });
    }
}

Until now I just get 404 not found.

Upvotes: 3

Views: 97

Answers (1)

slashingweapon
slashingweapon

Reputation: 11307

Your chief problem here is the your index function will only be called if the URI is /router/. The simplest solution is to name your method differently:

class Router extends CI_Controller {
     public function details($file) {
           $this->load->view($file);
     }
}

The URI for SampleLink.html would now look like: /router/details/SampleLink.html. This is very simple, and should work without any problems. Also, it shouldn't interfere with any other methods in that controller.

If you really don't like the longer URL then you can shorten it by implementing a _remap() method. But if you do this, remember that you are overriding all of the default method-mapping behavior of the controller.

With this implementation, you could use the URI /router/SampleLink.html. But that's all you could do. No other methods in the controller would be accessible.

class Router extends CI_Controller {
     public function _remap($file) {
           $this->load->view($file);
     }
}

Finally, if you wanted to use the custom mapping for files, but keep the usual function-mapping behavior of the controller, you could do something like this:

class Router extends CI_Controller {

     public function _remap($method, $args=array()) {

           $callable = array($this, $method);

           if ($method[0] != '_' && is_callable($callable))
               // If $callable really is a usable method in this class, then
               // go ahead and invoke it with the given $args array.  Make sure
               // to exclude method names starting with '_', which are supposed to
               // be kept private and inaccessible from the web.
               call_user_func_array($callable, $args);
           else  
               // Otherwise, look for a view with the name $method.  Hopefully,
               // this will be something like "SampleLink.html", which exists in
               // the views folder.
               $this->load->view($method); 
     }

}

Upvotes: 1

Related Questions