i wanna migrate my website into CI.
i just simply modified from ci sample file welcome.php
in index() function , i load the view to show.
however , i put many javascripts and css files in the header file .
and call it by $this->load->view('header');
but i can not load the javascript files correctly!
Can anyone give me some tips ? it;s hard to configure how to set the correct path.
<script type="text/javascript" src="/assets/javascripts/order.js"></script>
<script type="text/javascript" src="../assets/javascripts/order.js"></script>
<script type="text/javascript" src="../../assets/javascripts/order.js"></script>
my controller code as following
class Welcome extends CI_Controller {
public function index()
{
$this->load->helper('url');
$this->base = $this->config->item('base_url');
$this->load->view('header');
$this->load->view('welcome_message');
}
}
belows are my folder structure
Adding JavaScript and CSS (Cascading Style Sheet) file in CodeIgniter is very simple. You have to create JS and CSS folder in root directory and copy all the . js files in JS folder and . css files in CSS folder as shown in the figure.
Now create a folder named CSS in the public directory, and create a style. css fine and keep this file in the same folder you just created. Now create a js folder in the public directory, create a custom. js file, and keep the file in the same directory you have just created.
Auto-loading Models If you find that you need a particular model globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload. php file and adding the model to the autoload array.
If you want to create an assets folder in your CodeIgniter root directory, create an assets folder in root, open your assets folder, create a CSS file named style. css, put it into the CSS folder, and create your js file called script. js and keep it into js folder.
put your assets folder with applications, system, assets
not in application and simple load the url
helper class in controller where you call the header view part something like
$this->load->helper('url');
$this->load->view('header');
and simply use something like this in your header file..
because $this->base_url()
return the /
folder..
<script src="<?php echo $this->base_url();?>assets/javascript/jquery.js"></script>
Changing the folder structure because access within the application folder is just for the core part that i know..
here is the link if you want to know more about URL Helper
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With