Hikmah Tiar
Hikmah Tiar

Reputation: 13

CodeIgniter : Included JS or CSS in view folder

I'm include JS or CSS in Folder views codeigniter but experienced a

403 error,

How to solve it?

example code :

 <script type="text/javascript" src="<?php echo base_url() . APPPATH; ?>views/data/index.js">

or

 <link type="text/css" rel="stylesheet" href="<?php echo base_url() . APPPATH; ?>views/data/index.css">

then came the error "

NetworkError: 403 Forbidden

Kindly solution of masters.

Sorry if my english language is not good

Upvotes: 0

Views: 1542

Answers (4)

Hasnat Ghani Mirza
Hasnat Ghani Mirza

Reputation: 1

if its your requirement to have js or css file specific to view and you want to handle those file within view then: Simplest way is, create file

xxxcss.php and xxxjs.php //put your js code or css style

Define Variable if required

$data['variable']="Something"; // This is optional if you need pass php variable to your js or css before loading in page.

load view in controller and pass to another view or directly load within view

$this->load->view('css.php or js.php',$data);

Upvotes: 0

Indrasinh Bihola
Indrasinh Bihola

Reputation: 2125

Easiest Answer to your problem is to create new directory in your application root like this:

-Your_Application
    -application
    -assets <-- Create this folder.
      -css  <-- Create this subfolder inside assets folder.
      -js   <-- Create this subfolder inside assets folder.
        -script.js <-- Place your script files inside js folder.
    -system
    -index.php

Now you are able to access your css and js files using url helper like this:

<script src="<?php echo base_url('/assets/js/script.js'); ?>" ></script>

Follow same procedure for css and make sure url helper is enabled from config/autoload.php like this:

$autoload['helper'] = array('url');

Want to know the reason behind this:

The reason behind happening this is that your application outputs from index.php in your application root as CI implemented Front-controller design pattern.

So we don't need to place our public assets anywhere inside our application that's why CI restrict direct access to application and system folder from the public access.

Upvotes: 1

amit
amit

Reputation: 874

CodeIgniter comes with a .htaccess in the /application directory which contains

Deny from all

You'll need to remove this file or change it's ruleset to access js/css files etc from anywhere inside

application/*

It is however largely inadvisable to allow access entirely to the application.

Upvotes: 0

Mohmmad Ali
Mohmmad Ali

Reputation: 55

In your view you cannot have assets or supporting files on it. That wrong file structure of the codeigniter. You can have a folder name asssets on a root and follow this steps.

you can add url path on constraints.php on config folder .... you define the url like this on constants :

NOte: xxx is your site name

define('ROUTE_STIE_PATH','localhost/xxx/'); 
define('ADMIN_CSS_DIR_FULL_PATH',ROUTE_STIE_PATH.'assets/css/');
define('ADMIN_CSS_DIR_FULL_PATH',ROUTE_STIE_PATH.'assets/js/');

and then you can echo by simply on view or header

<link href="<?php echo ADMIN_CSS_DIR_FULL_PATH; ?>index.css" rel="stylesheet" type="text/css">



<script src="<?php echo ADMIN_JS_DIR_FULL_PATH; ?>index.js" type="text/javascript"></script>

Upvotes: 0

Related Questions