Codeigniter: File not found - php

Why do the paths to my css,js and img files have the CONTROLLER name BEFORE 'application'?
Path from firebug:
localhost/quote-generator/admin/application/assets/bootstrap/css/bootstrap.min.css
My code:
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css'); ?>" >
This is from a header.php file i have included in one of my views generated in my Admin controller in the 'login' function.
Any suggestions??

Its bad practise to keep assets files, i.e. css/images in the application folder
Your Codeigniter folder structure should be this
root
|
|_______________application
|_______________assets
|_________css
|_________images
considering the root is the admin
setting the above structure, set the base_url like this
$config['base_url'] = 'localhost/quote-generator/admin/';
then put the href like this
href="<?php echo base_url();?>assets/bootstrap/css/bootstrap.min.css"

use :
site_url()
instead of
base_url
it's saver that way

Related

How to make Dynamic css path?

I'm using not_found.php file for unfound files on server , and it's in Main Directory
it's style is in 'assests/css/main.css' from same Directory ,
so I make href of linking stylesheet
href="assests/css/main.css"
it works perfect if user types 'Main/wrongFile' for example , but if user typed 'Main/ez/wrongFile' , stylesheet is included wrongly due to changing of path
I'm searching for function like
$Path = FindPath('Main/assests/css/main.css');
so it gets that path wherever file is in
then I can simply
href="<?php echo $Path; ?>"
The best way to fix it is using the code below;
<link rel="stylesheet" href="/assests/css/main.css" type="text/css"/>
adding / at beginning of assests/css/main.css simply tells browsers which root directory to look at.
Option 2) Using htaccess. Add the code below into your .htaccess file
RewriteRule ^([^/]*)/assests/css/main.css$ assests/css/main.css [R=301,L,NC]

After redirecting the page CSS is not getting fetched in codeigniter

After redirecting the page the css and js which are stored in the assets folder, is not getting fetched in that redirected page.
Here is the code to redirect the page, the name of this page is interior.php
<a class="viewall pull-right" href="<?php echo base_url("details/profile/".$article->type_id); ?>">
The page it is getting redirected to is profile.php, here is the css link in this page:
<link rel="stylesheet" href="../assets/css/style.css"/>
Along with the css and js, I am also not getting the images stored in the img folder inside Assets folder. The assets folder is inside the application folder at the same level as views folder.
Instead of using relative URLs, use absolute URL. You can use base_url() to achieve this in the following manner:
<link rel="stylesheet" href="<?php echo base_url('assets/css/style.css'); ?>"/>
For the above code to work, you need to move your assets folder into the base directory (one level above application).

how to setup the assets folder in CodeIgniter like in Laravel

like the public folder given by laravel default i tried to create something like that for easy make up of my css, js, picture and so on. I tried configuring it like this. In my application/helpers/utility_helpers.php
<?php
function asset_url()
{
return base_url().'assets/';
}
?>
I have my assests folder along with application, system, user_guide etc.
i confugured the autoload function of application/config as :
$autoload['helper'] = array('url','utility');
when i hit any url in browser now, it says
An Error Was Encountered
Unable to load the requested file: helpers/utility_helper.php
how can i make it and can you explain me if i need to specify root for that helpers also.And if not can i simply access the assets like this:
<img src="assets/base_components/build/banner.png">
in my view
In fact the answer is very simple, you only need to put ../ before every file that is under assets folder, because the root of the codeigniter is in application folder, but how you can't put the assets folder there and for the best practices, let the structure of the project like below and apply this solution to the path of the files:
Website Folder structure
|---|application\
|-------(CodeIgniter application folders...)
|---|assets\
|-------css\
|-------images\
|-------js\
|-------robots.txt
|---|system
|-----(CodeIgniter system folder...)
|index.php
Path of the files
ex.:
CSS
<link href="../assets/css/bootstrap.min.css" rel="stylesheet">
<link href="../assets/css/plugins/font-awesome.css" rel="stylesheet">
IMAGES and others
<img src="../assets/images/build/banner.png">
JAVASCRIPT
<script src="../assets/js/jquery-2.1.4.js"></script>
<script src="../assets/js/plugins/jquery.scrollSpeed.js"></script>
Note.:
Always remember to applay the best practices to the code too (this will help for the performance of the the page too), like below:
<html>
<head>
meta tags
favicons
css files styles
</head>
<body>
page structure code
javascript files
</body>
</html>
Yo can do this instead:
<img src="<?= base_url('assets/base_components/build/banner.png') ?>">
Your code looks ok, it should work.
According to the error
Unable to load the requested file: helpers/utility_helper.php
there is no utility_helper.php file in application/helpers/ folder. Check if the file exists in that folder or if you don't have a typo in the file name.

including php files in a header that is itself included in files in different folder levels

OK, the title is a little confusing but this is the structure of my web site:
/
|--header.php
|--page1.php
+--css
|--style.css
+--subsection
|--page2.php
+--functions
|--classes.php
Right, so both page1.php and page2.php will include header.php. This I do by just using the relative file path from whatever php page to the header.php.
header.php itself includes other files; for example css/style.css and functions/classes.php.
I might include them like the following:
<link rel="stylesheet" href="css/style.css">
<?php require_once("functions/classes.php"); ?>
This will work for page1.php as the reletive paths are correct. For subsection/page2.php this will fail as the paths should be ../css/style.css and ../functions/classes.php but remain as they are defined in header.php.
My question is how can I get header.php to always use the correct relative file paths for its includes regardless of where the file calling header.php (e.g. apage.php) is located in the web site directory.
Set a base path to the css /functions :
define('CSS_BASE','insert/full/path/here');
then access css in header.php using
<link rel="stylesheet" href="<?=CSS_BASE;?>/style.css">
For php includes, making the path relative to header.php is ok.
For the css file, you should use an absolute path, like <link rel="stylesheet" href="/css/style.css">.
Edit : The css/javascripts files are not "include" ; those are html tags, so they will be processed client-side by the browser. That's the reason why the path should be relative to the url of the page where the html will be, not to its place on the server.
When I did this I set a variable (say $urlStart) at the start of each page (page1.php, page2.php) that contained the relative path of the main directory (eg page1.php would have $urlStart='./', page2.php would have $urlStart='../'). Then in your header.php, use this variable at the start of each URL. Like this:
<link rel="stylesheet" href="<?php echo $urlStart;?>css/style.css">
<?php require_once($urlStart . "functions/classes.php"); ?>
If you are including page2.php inside the page1 then your path will work. no need to go back to the previous folder.

How can I include the CSS file in CodeIgniter?

I have make a assets folder in root directory where application folder exist. now I have assets sibling to application.
Now when I tried to open http://localhost/CodeIgniter/assets/css/bootstrap.min.css in web-page it's not working.
Do someone can help me how I can got it worked.
In a solution I found that writing this line in config file will work but it's not work either by doing this change.
$config['base_url'] = 'http://localhost/CodeIgniter/';
Now please some guideline me how to load CSS file in CI.
solution :-
I have done something wrong in my codeigniter project. I download it again and try these thing again, without anything it's working.
thanks you all of guys. you save my time.
In constant.php:
define('URL','http://localhost/CodeIgniter/');
define('IMG',URL.'assets/img/');
define('CSS',URL.'assets/css/');
define('JS',URL.'assets/js/');
In config.php:
$config['base_url'] = URL;
In view:
<link rel="stylesheet" href="<?php echo(CSS.'bootstrap.min.css'); ?>">
Hope that helps somewhat.
define general css that used.
Open "config.php" within directory CodeIgniter\system\application\config.
$config['css'] = 'mystyles.css';
Create a file named mystyles.css within root application: \CodeIgniter.
$this->load->helper('url');
$data['css'] = $this->config->item('css');
You can load css in views pages like this.
Add below line in your controller action /application/controllers/yourController.php
$this->load->helper('url');
Then add below line to your view file's head tag.
<link rel="stylesheet" type="text/css" href="<? echo base_url('assets/css/yourcssfile.css');?>" />
Assuming that you have assets/css/ folder is created in your app directory.
<path to your app folder>/assets/css

Categories