Css, JS Inclusion Method - php

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?

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.

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/

Related

Klein system. CSS isn't working

I'm testing Klein routing system, https://github.com/chriso/klein.php
it's awesome but i can't get my css ant images to run
This is my directory structure:
index.php
.htaccess
vendor
views
includes
assets
css
images
And here's one of my includes line of code where i try to access my assets/css:
<link href="../assets/css/main.css" rel="stylesheet">
I tried everything. Every possible hint would be great, thanks.
EDIT:
now i think it can be problem in my htaccess. how to make that url won't rewrite if it's css or img?
my .htaccess :
RewriteEngine on
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
Your link is most likely wrong (unless the code you pulled it from is in the include or assets folder). the ".." literally means "up one level". So if you code is in the .htaccess folder (which it usually is I believe) or any folder on that level the correct link would be:
href='../views/assets/css/main.css'
If your code is in css or images (not sure why it would be) the correct link would be:
href='../css/main.css'
Good lcuk!

Request for resources (CSS, JS) being incorrectly redirected by htaccess rewrite rules

I had a look at a few possible duplicates for this question but rewrite rules are fairly specific to projects so I couldn't find a suitable answer.
Crux of issue? Requests for certain files (CSS, JS & images) are being rewritten (I think) by the htaccess file.
I have an htaccess file set up to direct as follows:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?([a-zA-Z]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&id=$3 [NC,L]
It takes a URL like localhost/framework/booking/dashboard and rewrites to localhost/framework/index.php?controller=booking&action=dashboard
The relevant file structure inside my local www folder looks something like:
framework
-index.php
-htaccess
-views
-Booking
-dashboard.php
-wuxia-blue.css
The CSS link in dashboard.php is as follows:
<link rel='stylesheet' type='text/css' href="wuxia-blue.css">
The HTTP request being made seems to be:
/framework/booking/wuxia-blue.css
It should be:
/framework/views/booking/wuxia-blue.css
Ideally, I'd like to use something similar to
href="<?php echo(CSS . "wuxia-blue.css"); ?>".
Where CSS is a defined constant in the application and where wuxia-blue.css (and all other css files) are contained in a predefined folder of stylesheets. Obviously this would apply to other assets like .js files and images.
Any ideas?
The htaccess rule is correct - the problem is with your folder structure.
When you create a relative link, it's relative to the current URL as displayed in the address bar - the location of the file you're in isn't relevant.
Your view file is at /framework/views/booking/dashboard.php, but the browser just sees the url /framework/booking/dashboard. That means the browser interprets the location of the resource to be /framework/booking/dashboard/wuxia-blue.css
Personally, I'd recommend just keeping all your css and images outside of the framework in /images and /css. Otherwise, may have to add some code in your framework to serve up image files, since all the image requests will be running through your framework.php file.

My php controller can't include css files

The following issue is driving me crazy, perhaps i am thinking to difficult.
Here is the thing, i developed a small MVC framework that works fine. Atleast without url rewriting.
The problem is that as soon as i use url rewrite, things like the css or images which are included in the templates are directed to the wrong directory.
If i type for example: http://www.domain.com/home then everything is fine and the css file get loaded from the http://www.domain.com/css/ directory.
But when i type: http://www.domain.com/home/ the css file won't be loaded, since its looking for the css files in http://www.domain.com/home/css/ Which is obliviously the wrong directory. It seems it sees home/ as a directory where it looks for the the included files.
If i don't use any url rewriting at all, just by typing: http://www.domain.com/index.php?slugs=home/ then there are no problems at all. So i don't think the problem is caused by my script so thats why i think the problems should be looked for in the .htaccess file.
Here is my htacces file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteRule ^(.*)$ /astrostrategy/index.php?slugs=$1 [L]
Is there a way, that home/test/whatever(The segments) are not seen as a directory?
Hope my post made sense, i often think to complicated :P
Already thanx for any help! :)
Gr,
Hermes.
If Death's answer doesn't work you can always set the base tag to the url to use in relative paths
it seems you're using relative to currect directory path for css. when you use http://www.domain.com/home/ browser takes home as directory and css file will be located in http://www.domain.com/home/css/. my suggestion is use relative to root path. Now you're using something like :
<link rel="StyleSheet" type="text/css" href="css/style.main.css" />
it's better to use absolute path or relative to root path.
<link rel="StyleSheet" type="text/css" href="/css/style.main.css" />

error including js files in codeigniter

Am new to codeigniter,Am using the version of 1.7.2.i have installed freak auth in that..i want to have my another application running in that..I have copied the contents in the root folder and created view and controller..In my view book.php my js contents getting included but it is not displaying...am getting only blank screen in the browser,
i think the problem is with the script tags;my code is
<script type="text/javascript" src="<?php echo base_url();?>files/object.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>files/address.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>files/facebook.js"></script>
where am going wrong?
I had the same problem until i found out I didn't autoload url-helper. Weird thing is that several different browsers didn't output any error messages at all when looking at the html source if the errors occured inside the include statement.
Also remember to allow direct access to that dir in your .htaccess file if you use one.
Include the url-helper in your application/config/autoload file like this:
$autoload['helper'] = array('url');
Or do it on the fly in your controller
$this->load->helper('url');
If .htaccess is blocking access to the file you can exclude the files dir:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|stylesheets|javascript|files)
RewriteRule ^(.*)$ index.php/$1 [L]
Unless /files is an actual directory or your Files controller has a _resolve method, each of those should be a 404.
If it is a directory, then you need to confirm that you can read anything in that directory from the browser. There may be permissions issues or Apache may believe that that directory should be marked inaccessible.
On the other hand, if you have a File controller, then you are saying, call the object.js method on the File controller. _resolve lets you avoid this because it serves as a catch-all -- call the _resolve method on the File controller and tell it that we're trying to display object.js.
As to your organization of views and the like after that, well, I can't really help you there without knowing more about the controller and view in question.
First thing that comes to mind are in order:
1) url-helper not loaded
2) htaccess is blocking treating your directory as a controller
Solutions
1) Add url in config/autoload.php
eg
$autoload['helper'] = array('url');
2) Add files to the htaccess exeptions
eg
RewriteEngine on
RewriteCond $1 !^(index\.php|files)
RewriteRule ^(.*)$ /index.php/$1 [L]

How to tell code igniter to ignore /lib/css path

How can I tell CodeIgniter to ignore the /lib/css folder and load my stylesheets instead of trying to load things via my controller?
I'm just going to take it straight from the user guide:
By default, the index.php file will be
included in your URLs:
example.com/index.php/news/article/my_article
You can easily remove this file by
using a .htaccess file with some
simple rules. Here is an example of
such a file, using the "negative"
method in which everything is
redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request
other than those for index.php,
images, and robots.txt is treated as a
request for your index.php file.
With .htaccess: You decide which files and folders can be accessed directly, like css or images.
Without .htaccess: All references to the application are routed through index.php so there is no possibility of conflict
In the example above, two files and one directory (images) are allowed direct access. Any other requests are routed through Codeigniter, so if you HAVE a controller called css and want to access it - you can do so by removing it from this list. If you have a directory named css, you would add it to this list of exceptions and be able to access it, and all files within it, directly.
Another note: do not try to use ../../relative/paths with CI, use full URLs or /absolute/paths. Use the helpers that exist, like base_url(), and link_tag() as seen in Phphelp's example
Your question is not exactly clear. If this is what you want, you can do the following.
echo link_tag('css/mystyles.css');
Gives:
<link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />
In place of mystyles.css, you can give your style sheet.

Categories