error including js files in codeigniter - php

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]

Related

How to link to files and change base url in CodeIgniter?

Hello I've been running into a few issues with CI lately. When I want to link a css file to pretty much anything, I need to put it directly into the root folder. If I move the file somewhere further in the hierarchy it won't load it. Here's an example of when I am trying to link this core_css.css. It loads the one in the htdocs folder, but not the one in the css folder.
Works:
<link href="<?php echo base_url(); ?>core_css.css" type="text/css" rel="stylesheet" />
Does NOT work:
<link href="<?php echo base_url(); ?>application/OBS/css/core_css.css" type="text/css" rel="stylesheet" />
I've tried putting the css filed in all the other subfolders and linking those, but it only loads the one in the root.
My second problem is that every time I try to test a controller, I need to access it through index.php like this:
http://localhost:8888/index.php/test_controller
Is there a way to get rid of the need to put the index.php in the URL?
For your first problem:
Add .htaccess file in your root web directory and write allow from all it means, in this folder your all files and all folder will not give error Access forbidden! use it like this:
<link href="<?php echo base_url(); ?>application/public/css/style.css" rel="stylesheet" type="text/css" />
N.B:
I usually put all my files like that into an "assets" folder in the application root, and then I make sure to use an Asset_Helper to point to those files for me. This is what CodeIgniter suggests.
For your second problem:
If you are using Apache place a .htaccess file in your root web directory containing the following:
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
for better understand you can see codeigniter URL section through this link
http://ellislab.com/codeigniter%20/user-guide/general/urls.html
and another one is http://snipplr.com/view/5966/codeigniter-htaccess/
For the second question: put the following in an .htaccess file inside your CodeIgniter root folder (htdocs in your case):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
Make sure to set $config['index_page'] to an empty string, and also set $config['base_url'] accordingly (these two options are inside /application/config/config.php)
For the first question: try to make a folder called assets, or something similar and put it in the root of the site (again inside htdocs). Move your css file there, and try to access it by visiting http://localhost:8888/assets/core_css.css.
Also, in case you are using Linux box, make sure the folders you want to access have read permissions.

external js & css not loading in codeigniter

To load external Javascript file in codeignitor, I'm using
<script src="<?php echo base_url("application/js/contentslider.js"); ?>" type="text/javascript"></script>
I can see in the page's source as
<script src="http://localhost/cinifb_ci/application/js/contentslider.js" type="text/javascript"></script>
which means to my knowledge that the file is loading perfectly similarly for my CSS file, I've used
<link rel="stylesheet" href="<?php echo base_url(); ?>application/css/default.css" type="text/css" />
and in the page's source code its showing as
<link rel="stylesheet" href="http://localhost/cinifb_ci/application/css/default.css" type="text/css" />
which means even its loading perfectly. (on clicking the external js/css link in the source its showing 403 Forbidden). But neither of their effects are applied on the page. No error its showing.
I've loaded URL helper in controller as $this->load->helper('url');
and placed $autoload['helper'] = array('url'); in autoload.php
can you please explain me where I went wrong.
I learn that to load external JS, we need to install jquery-codeigniter library. I've downloaded them and included the respective files into respective similar folders of my application folder.
Now when I try to include $this->load->library('jquery'); in my controller, its showing
An Error Was Encountered
Unable to load the requested class: jquery
Hence I request you to please help me to understand why 403 Forbidden is showing when I can see its pointing to the exact location of the JS/CSS file and secondly if the installation of the jquery-codeigniter library is wrong please guide me.
Note: http://ellislab.com/codeigniter/user-guide/libraries/javascript.html didn't help me
Edit:
jquery-codeigniter library i've placed in system/libraries/javascript/
In systems .htaccess file, I've written as
RewriteEngine on
RewriteRule ^$ /index.php [L]
RewriteCond $1 !^(index\.php|images|css|js|video_files|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /cinifb_ci/index.php/$1 [L]
In the view I'm writing as
$this->load->helper('url');
<script src="<?php echo base_url("application/js/jquery-1.8.2.js"); ?>" type="text/javascript"></script>
In controller as
$this->load->helper('url');
$this->load->library('javascript');
$this->load->library('javascript/jquery');
$this->load->library('Jquery_ext');
again if I look at source its pointing to correct source but still if i click external js file link, its showing 403 Forbidden error
Sorry for lengthy post :(
Suggestions please ..!
A 403 hidden error means that the Web Server is rejecting the request to that particular resource.
I am going to assume you are using the provided .htaccess file to get pretty urls.
In the line that
RewriteCond $1 !^(index\.php|images|robots\.txt)
Make it read
RewriteCond $1 !^(index\.php|images|robots\.txt|application/js|application/css)
Actually, the above could be wrong, regardless, the idea is there that you need to add your css and js folders to the RewriteCond exception list
The best way is to create folder assets, and then you need to put css, images, js etc.
In this folder save the js/contentslider.js and finally in your header type:
<script type="text/javascript" src="<?php echo base_url();?>assets/css/js/contentslider.js"></script>
Don't forget, you need to set base_url() in config.php.
Thanks
you should include js folder into your .htaccess
if your .htaccess file is inside your application folder then copy it and paste it outside application folder mean root then open it and past code that i have mention and u just need to change name sample in line 3 with your website name or your main folder
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
UPDATE

mvc configuration on web server

I recently uploaded my codeigniter project onto my web server.
it's in a folder called project1 this is what my directory view looks like
[www.myurl.com]
|-> project1
|-> application
|-> css
|-> images
|-> js
I am having two problems.
The first is that I want it to load my welcome view when I go to (www.myurl.com) and not
(www.myurl.com/project1/main_controller/index.php/welcome). How do i get rid of all that garbage?
The second problem is when I use <?php echo base_url();?> it shows up fine in view source but when I click on it, it echo's the url [www.myurl.com/project1/www.myurl.com/project1/] why is it doing this?
It looks like you've installed CodeIgniter into a subdirectory. If you want your CI site to load from the root url, you should copy it one directory up so the Application & System folders are directly under the root url of your web directory. From there, the .htaccess rules that are posted by shail (and quoted below) should take care of removing the index.php in your paths.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ ci/index.php/$1 [L]
Regarding the site_url() issue, make sure you have your $config['base_url'] variable set in application/config/config.php and that it contains the trailing slash.
$config['base_url'] = "http://www.myurl.com/";
To test this further, try passing a parameter to the site_url() function like this:
echo site_url(welcome);
Which should return: http://www.myurl.com/welcome if everything is set right.
Getting CI up and running is rather simple. Most of it requires you to edit a few configuration
files.
You need to setup CI to point to the right base URL of the app. To do this, open up system/application/config/config.php and
edit the base_url array item to point to your server and CI folder.
view plaincopy to clipboardprint?
$config['base_url'] = "http://localhost/ci/";
Currently, the CI setup will have a default controller called “welcome.php”; you can find this in the system/application/controllers folder. For this tutorial, delete it and open your system/application/config/routes.php file. Change the default array item to point to the “helloworld” controller.
view plaincopy to clipboardprint?
$route['default_controller'] = "Helloworld"
CI also has a view file that we do not need. Open up the system/application/view/ folder
and delete the welcome_message.php file.
You can change your default controller here.
In order to edit Url http://localhost/ci/index.php/helloworld/.There are a few things you can do to improve your CodeIgniter experience -
like removing that annoying index.php bit out of the URL. You can accomplish this task by creating a .htaccess file in the root folder, and adding the following code.
RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ ci/index.php/$1 [L]
You’ll also need to open up the config.php file in system/application/config/ and edit the index_page array item to a blank string.
view plaincopy to clipboardprint?
$config['index_page'] = "";
Another nifty trick is to turn on CI’s ability to parse PHP alternative syntax if its
not enabled by the server. To do this, open up the same file as before, system/application/config/config.php, and set the rewrite_short_tags array item to TRUE.
view plaincopy to clipboardprint?
$config['rewrite_short_tags'] = TRUE;
I hope all goes well!

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/

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