What is the best way to call files?

3

Well, I use codeigniter (but not specifically in it, anyone who is MVC). I wanted to know the best way to call my files, css, js, fonts, etc. I think the best way is to call the controller method, but let's assume that for each page (method) I want to use a certain plugin code Js but in the other not, how would I do it?

One day I made some conditionals (php), checking if the URI (controller name) is such load that file (css, js, etc), and that in footer.php of my template. In case this would be the best alternative?

Thank you!

    
asked by anonymous 06.03.2017 / 22:25

2 answers

1

Your basic idea of conditioning by the controller name is correct (in my opinion), but execution can be improved. Use file_helper.php to help you perform the task. It's relatively simple: Extend the helper as in the documentation :

Create MY_file_helper.php in application / helpers / and put the following in there:

if(!function_exists('js_loader')){

/**
* Carrega os scripts por demanda, a depender do controller ativo e do 
* diretorio definido
* @return string
*/
function js_loader() {
    //Captura a instancia do CI
    $ci = & get_instance();
    //Captura o controller ativo
    $controller = $ci->router->fetch_class();
    //Declara o diretorio onde estao os scripts do $controller. 
    //Este diretorio precisa estar acessivel ao navegador
    $jsdir = "assets/js/$controller/";
    //Checagem do $jsdir
    if(file_exists($jsdir)){
        $DirectoryIterator = new DirectoryIterator($jsdir);
        echo "<!--Scripts do controller $controller-->\n";
        foreach ($DirectoryIterator as $entry) {
            //Carrega apenas arquivos (exclui diretorios...)
            if ($entry->isFile()) {
                //Carrega apenas arquivos '.js'
                if (in_array($entry->getExtension(), ['js'])) {
                    $file = base_url($jsdir.$entry->getFilename());
                    echo "<script src='$file' charset='UTF-8'></script>\n";
                }
            }
        }
    }
   }
}

Load the helper into the CodeIgniter instance (or use autoload ) and call the function anywhere in the view :

<?php $this->load->helper('file'); ?>
 <html>
  <body>
   <?= js_loader() ?>
  </body>
</html>

Basically: all .js scripts that are within "assets/js/$controller/" will be loaded when $controller is active.

    
11.03.2017 / 13:24
0

The best practice would be to leave the resource calls in View, as it is the one they will serve.

Just as @leonardopessoa commented in your question, you can create templates that provide common parts to the main template, for example:

application / views / header.php

<!DOCTYPE html>
<html>
<head>
    <title>Hello World - Template</title>
    <link rel="stylesheet" href="folha_de_estilo_main_para_todos.css">
</head>
<body>

applications / views / footer.php

    <script src="script_main_para_todos.js"></script>
</body>
</html>

applications / views / page_personal.php

<p>
    Hello world - Template do método personal!
</p>

applications / views / page_contact.php

<p>
    Hello again, world - Template do método contact!
</p>
<script src="script_customizado_apenas_para_este_template.js"></script>

applications / controllers / pages.php

class Pages extends CI_Controller {

public function index() 
    {
        $this->load->view('header');
        $this->load->view('page_personal');
        $this->load->view('footer');
    }
}

public function contact() 
    {
        $this->load->view('header');
        $this->load->view('page_contact');
        $this->load->view('footer');
    }
}
Well, I do not think there is an ideal way, but in my opinion, this would be the best way to organize the resources in an MVC application, especially applications with CodeIgniter, which does not provide such cool legal helpers to do this.

  

I used code snippets from the following link to write this answer, in it, you could find another way to implement templates in CI, but I did not find anything interesting: link

    
07.03.2017 / 04:03