2

It's a conceptual question. What's the best way to inlude assets like CSS, Javascript to HTML page.. I'm implementing my own MVC framwork. Application directroy structer is

index.php
controllers
  c1.php
  c2.php
    ...
views
  v1.php
  v2.php
    ...
scripts
  s1.js
  s2.js
    ...
styles
  style1.css
  style2.css
    ...

As you can see, all request come through index.php and then I find the right control to handle it.. Controller process some business logic then include a view file..

In view file i need to give an absolute path to all css and java script like this ;

<link rel="stylesheet" type="text/css" href="<?php echo APPROOT; ?>/styles/master.css" />'
<script type="text/javascript" src="<?php echo APPROOT; ?>/scripts/jquery-1.6.1.min.js"></script>

APPROOT is a constant which defines directory path for application:

define("APPROOT", "/project1");

I think It's not the best way so how can i improve it?

0

2 Answers 2

2

Typically you would want to keep your application outside of the public directory, and create an include path that leads to it. This way you can write your url rewrite to simply redirect to index.php only if a file at the requested path does not exist. In htaccess, that would look something like this.

# redirect any requests for missing files to index.php
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [L]

This way if the file being requested does exist in the public directory, apache will simply serve that file as usual, but if the file being requested does not exist, it will redirect to index.php for MVC handling.

To create an include path that leads to your application directory, see the following documentation:

http://php.net/manual/en/function.set-include-path.php

You can also set include paths via inis.

Sign up to request clarification or add additional context in comments.

Comments

1

In my opinion the right way is to do as Zend does. It may or may not be compatible with your code.

$this->view->headLink()->appendStylesheet($this->view->baseUrl().'/css/style.css');
$this->view->headScript()->appendFile($this->view->baseUrl().'/js/jquery.js','text/javascript',array('language'=>'javascript'));
but i doubt your way, @dqhendricks have good opinion and i think you should look at this tutorial and reconsider your framework approach. http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/

4 Comments

Ok I see the point in Zend framwork.. It's also similar to Codeigniter.. As I understand that there is no way giving relative path to source of css or js files.. Link you shared is very useful, thanks. Btw I'm calculating absulute path for css and js with APPROOT + "styles/x.css". Should I change it with base url like zend or codeigniter? If so how can I calculate baseurl?
Yes, your point is valid. I was thinking to include this but thought may be irralavant. Application Path and Base Url are to entirely different things. And Yes you need to use absolute path not relative. It will save time as well as effort and will eliminate any related future problems.
You also can use base tag in head, its useful. And i read that article sometime ago, read it its worth reading. You will find your answer in it regarding how to calculate base url.
I already using base tag in header section.. I'm using it to make a ajax post request in js.. Othwerwise it's because js files are under scripts directory and my ajax file in let say ajax directory, I cant access ajax directory but with base tag ; I can write : var ajaxFile = $('base').attr('href')+ "/core/ajax.php"; Then just $.post( ajaxFile, //Ajax file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.