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

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.

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.

Hide file extension with htaccess

I'm not the best with mod rewrite so if anybody can help me out here that would be great.
I'm using a markdown processor script and it's using rewrite to grab any files that end with a markdown file type. However, I'd like this script to grab any files within a folder, rather than any files that end with the markdown file type.
Here's the htaccess:
# display Markdown as HTML by default
RewriteEngine on
RewriteRule .+\.(markdown|mdown|md|mkd)$ /static/includes/markdown/render.php
RewriteRule .+\.(markdown|mdown|md|mkd)\-text$ /static/includes/markdown/render.php [L]
Is there a way to grab all files within a folder called (let's say) "folder" and eliminate the file type on the end?
So maybe have a URL like
website.com/home
that actually is
website.com/home.md
and is processed with the markdown script?
Hope this makes sense.
The re-write module and it's .htaccess files actually work on a per folder basis. Usually one would have a main .htaccess file in the web root of a site/server. However you can add numerous .htaccess files throughout your site's folder structure giving each individual folder specific rules.
All you would have to do is add another .htaccess file to your markdown folder and enable it to parse URL's without file extensions, forwarding it to a script which will be able to detect what original file was requested -
RewriteEngine on
RewriteRule ^(.*)$ /static/includes/markdown/render.php?file=$1 [L,QSA]
Basically what is happening here is that any file requested within this folder will be passed through your render.php file.
Now in your render.php file, you would have a $_GET parameter of file containing the original URL. For a url of http://example.com/markdown/foo, your render.php would have foo in the file parameter -
/static/includes/markdown/render.php?file=foo
If you set the correct headers in render.php it will be able to print out any format of file, hiding it's extension in a "fake" URL.

Css, JS Inclusion Method

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/

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]

Categories