Welcome Guest, Not a member yet? Register   Sign In
Dynamic Assets. Am I doing this right?
#1

[eluser]Unknown[/eluser]
For the application I'm writing, I need to be able to have css and javascript files that can be processed by php. The files live in application/assets/{js,css} I ended up writing this route:
Code:
$route['assets/([^/]+)/(.*)'] = 'assets/load/$1/$2';

and this controller in application/controllers/assets.php:
Code:
<?php defined('BASEPATH') or exit('No direct script access allowed');

class Assets extends CI_Controller {
  function load(/* varargs */) {
    ### <Varargs> ###
    $varargs = func_get_args();
    $type = array_shift($varargs);
    $filename = implode('/', $varargs);
    ### </Varargs> ###

    $include = "../assets/{$type}/{$filename}.php";

    if(is_file(APPPATH . "/views/{$include}")) {
      $this->load->view($include);
    } else {
      show_404();
    }
  }
}

Now I can access application/assets/css/site/test.css.php in my browser at http://localhost/ci/index.php/assets/css/site/test.css with no issue.

What I am wonder is if I reinvented the wheel on this one, or if there is a "best practice" to follow for this sort of thing.




Theme © iAndrew 2016 - Forum software by © MyBB