Welcome Guest, Not a member yet? Register   Sign In
Menu system based on model names - how?
#1

[eluser]Jaketoolson[/eluser]
Currently, I am porting over a web system built entirely in PHP (no MVC, little OOP) over to CI. The primary use of this site is not CRUD, but mainly model-based algorithms. The main focus of the site is providing graphs, charts, and raw data to customers all of which is driven by forms the user interacts with. This data needs to be email-able, download-able in CSV format, and more which is why I'm moving it to a MVC method.

The site navigation menus are directly link to the name of models. Heres an example of my controller code (using this url example: http://site.com/index.php/reports/model_name) :

Code:
class Reports extends MY_Controller
{    
    function __construct()
    {        
        parent::__construct();
                  
        $this->options = array();
        $this->data = array();
        
        /** Models based on URI are loaded, so extract from URI **/
        parse_str(substr(strrchr($_SERVER['REQUEST_URI'], "?"), 1), $_GET);
        $this->options['uri'] = $this->security->xss_clean($this->uri->uri_to_assoc(4));
        $this->options['uri']['controller'] = $this->security->xss_clean($this->uri->segment(1));
        $this->options['uri']['action'] = $this->security->xss_clean($this->uri->segment(2));
        $this->options['uri']['id'] = @$this->security->xss_clean($this->uri->segment(3));

        /** Requested Controller Function **/
        $action = $this->options['uri']['action'];

        /**
         *  TODO: Need to just redirect to myaccount or login if the model doesn't exist.
         */
        if (file_exists(APPPATH.'models/'.$this->options['uri']['id'].EXT))
        {
            $this->model_name =  $this->options['uri']['id'];
            $model_name = $this->model_name;
        }
        else
        {
            redirect('/');
        }

        /** Load the model **/
        $this->load->model($model_name);

Is there a way I can pull the names of the models dynamically and generate the sites navigation? Is there a better way I can go about creating a dynamic menu based on model names? I'd just hate to maintain an array manually of the file names myself.
#2

[eluser]bubbafoley[/eluser]
You can use the directory helper to get the contents of a folder.

If we're still in your controller and assuming there are no models in subdirectories:
Code:
$this->load->helper(array('directory','inflector', 'html', 'url'));
$map = directory_map(APPPATH.'models/');
$models = array();
foreach($map as $key=>$val)
{
    $model_name = str_replace(EXT, '', $val);
    $models[site_url('reports/'.$model_name)] = humanize($val);    
}
$this->nav = $this->load->view('nav', array('models'=>$models), TRUE);

then in the view nav.php
Code:
<ul class="nav">
    &lt;?php foreach($models as $key=>$val): ?&gt;
    <li>&lt;?php echo anchor($key, $val) ?&gt;</li>
    &lt;?php endforeach ?&gt;
</ul>
#3

[eluser]Jaketoolson[/eluser]
wow that directory helper is AWESOME. I can move files into subfolders within the model folder and this would also reflect the menu/submenu structure! Thanks for this info!

also inflector helper is another one I've yet to use and would have never considered it for this. Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB