How do I load css files with a view in CodeIgniter? - php

I am currently trying to load a css file into a CodeIgniter view in the follow manner:
<link rel="stylesheet" href="<?php echo base_url(); ?>css/style.css">
A blank page is loaded. It seems that when I call the base_url function in the view the page doesn't load because the same thing happens when I call base_url() else where in the view but the page actually loads without css when I remove the calls to that function. Is there something that I need to require in order for the page to load?

From: http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html
This helper is loaded using the following code:
$this->load->helper('url');
The following functions are available:
...
You need to first load the helper either in the method called, or in the class constructor.

Try import annotation.
<style> #import url('<echo base_url()?>/css/styles.css'); </style>

You could do like this:
<link href="<?php echo base_url('assets/css/bootstrap.min.css') ?>" rel="stylesheet">

Related

Loading css and javascript in codeigniter

I am creating Web app in codeigniter.
When I link css and javascript file in my VIEW and runs on server it gives plain text.
My css style can't apply on the page. Even with the javascript file also.
Is there any configuration required !!!
you mentioned in your comment that you are calling css like but codeigniter doesn't take full path like that. you need to use base_url() for getting external css and JavaScript files.use below example for call css and js files
<link href="<?php echo base_url(); ?>folder/style.css">
<script type="text/javascript" src="<?php echo base_url(); ?>folder/abc.js"></script>

Codeigniter: files not found within header when included in view

I have a function in my admin controller called "login" which uses the 'login.php' view in application/views/admin/. I have the 'header.php' and 'footer.php' views included at the top and bottom of this file as shown below:
<?php include ('/application/views/layout/header.php'); ?>
The header and footer are included correctly in all my other views, but when included in the 'login.php' view, the asset files are not found. Here is my 'header.php' view:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="./application/assets/bootstrap/css/bootstrap.min.css" >
<link rel="stylesheet" type="text/css" href="./application/assets/style.css" >
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="./application/assets/bootstrap/js/bootstrap.min.js" ></script>
<title><?php echo $page_title; ?></title>
</head>
<body>
<div class="container-fluid text-center">
<div class="row">
<div class="span12">
<img src="./application/assets/signature.jpg" class="img-responsive center-block" style="margin-top: 20px;">
</div>
</div>
</div>
Here is the file structure of my application:
application
-+assets
---+css
------style.css
-+controllers
----admin.php
----page.php
-+views
---+layout
------header.php
------footer.php
---+admin
------home.php
------login.php
----home.php
I have used firebug to try and solve the problem, and the console is giving me a 404 error. It is adding the controller name 'Admin' into the filepath of the css file for some reason as seen below, as well as my .js files and the image:
/my_project/admin/application/assets
Any ideas as to why this could be? I have used mod_rewrite on the project in my .htaccess file.
For including header and footer I think better way this answer:
adding header and footer codeigniter to controller
...
And for to refer to CSS and JS file use CodeIgniter URL helper. You must load the helper And use this helper:
https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html.
Config base_url() in config.php file. Then link to your CSS and JS like this answer:
Code Igniter simple base url css
If you use Codeigniter framework I recomend to use a standart function loading view instead include() function.
For example, Codeigniter support function View() to load your HTML code at web page.
Call function like as:
$this->load->view(template);
At your case you need to put next code at your controller file login.php, where you want load header template. Look like as:
$this->load->view("layout/header.php");
Also, dont use incude(); The better write: include_once();
If you will work furthe with inheritance classes you have problems.
Read more about here: enter link description here

Loading CSS, JS in CodeIgniter

I am kinda newbie to CodeIgniter. Somehow I set up the development environment and ready to go. But loading the CSS, JS into page seems a lot more confusing. Before moving, I googled a lot but the base_url solution is not helping.
Part of View :
<link href="css/bootstrap-responsive.css" rel="stylesheet">
<link href="css/jquery-ui-1.8.21.custom.css" rel="stylesheet">
<link href='css/fullcalendar.css' rel='stylesheet'>
<link href='css/fullcalendar.print.css' rel='stylesheet' media='print'>
<link href='css/opa-icons.css' rel='stylesheet'>
<link href='css/uploadify.css' rel='stylesheet'>
Controller :
public function dashboard()
{
$this->load->helper('url');
$this->load->view('templates/css_styles');
$this->load->view('templates/top_bar');
$this->load->view('templates/left_menu');
$this->load->view('pages/dashboard');
$this->load->view('templates/footer');
$this->load->view('templates/js_end');
}
public function index()
{
$this->load->helper(array('form'));
$this->load->view('login');
}
route.php :
$route['default_controller'] = "welcome/dashboard";
$route['404_override'] = '';
$route['dashboard'] = 'welcome/dashboard';
If I put the dashboard method as default controller, the page is loading fine. But when I change it to just welcome, which is a login form and redirect to welcome/dashboard, the page doesn't load any CSS. Just all HTML texts. The dashboard page is just pure HTML, no PHP code written yet.
Please help me what might causing this.
What's wrong with base_url()?
<link href="<?php echo base_url('css/bootstrap-responsive.css')?>" rel="stylesheet">
make sure you load URL helper, I recommend autoloading it: go to application/config/autoload.php and add it to the helper array:
$autoload['helper'] = array('url');
You can use the link_tag() in html_helper.php to help get the paths correct.
First load the helper (I usually have it autoloaded)
$this->load->helper('html');
Then you can reference css like this:
echo link_tag('css/bootstrap-responsive.css');
The advantages are the function tries to build the correct absolute URL to the css file and it automatically sets the rel attribute to stylesheet and the type attribute to text/css.
Also you should have Chrome, Safari, or Firebug for Firefox so that you can inspect the code and see if the CSS files are being loaded or if they are not found. This will help with debugging.
Make sure you have a leading slash before your css folders:
<link href="/css/bootstrap-responsive.css" rel="stylesheet">
And make sure that the files are indeed located in /css off your web root, not inside the application folder.
first you need load helper url
$this->load->helper('url');
and then on controller
$this->data =site_url()."your css file";
then on your view
<link rel="stylesheet" type="text/css" href="<?php echo $css; ?>">
its simple.you can view full article load css/js

Codeigniter Link to Stylesheet

I am trying to learn Codeigniter. I have built a simple MVC following the tutorials on the CI site. This is my controller...
<?php
class Pages extends CI_Controller {
public function view($page = 'home'){
if(!file_exists(APPPATH.'/views/pages/'.$page.'.php')){
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
Here is the header template...
<html>
<head>
<title><?php echo $title ?> - WurkWithUs</title>
<link rel="stylesheet" href="http://localhost/ci/CodeIgniter_2.1.2/css/bootstrap.css" type="text/css" media="screen"/>
<link rel="stylesheet" href="http://localhost/ci/CodeIgniter_2.1.2/css/custom.css" type="text/css" media="screen"/>
</head>
<body>
<h1>Wurk With Us</h1>
The style sheets work when I use an absolute path like the one above. However if I try to use something like...
<link rel="stylesheet" href="<?= base_url()?>css/bootstrap.css" type="text/css" media="screen"/>
I get a blank page. When I "view page source" my style line looks like...
<link rel="stylesheet" href="
I have tried to follow the suggestions here and I get the same results i.e. document stops being parsed at the "href=" line.
What am I doing wrong? Is there a better way to load my stylesheets?
I've had this issue many, many times. I'd bet money you're not loading in the URL helper. It does not autoload, so either load it in your controller or autoload it.
If you choose to lead it in your controller, call
$this->load->helper('url');
before you load the view.
The reason you see the href blank in the source is because there was a PHP error at that point (it could not find the method base_url())
Try this:
<?php base_url("css/bootstrap.css"); ?>
Thanks
Try this in your controller
$this->load->helper('html');
and this in the head tag
<?=link_tag('css/style.css')?>
I think you need to load the "url" helper.
Load your helper in autoload.php
Like that way :: $autoload['helper'] = array('url');
This will auto load your helper every time. So, you need not to load it again and again in every controller.
or
If You want to load this helper for specific controller, then load the helper in your controller call
Like that way :: $this->load->helper('url');

Fatal error: Call to undefined function base_url() in C:\wamp\www\Test-CI\application\views\layout.php on line 5

Hello I am new to CodeIgniter and PHP, I am trying to setup it for the firs time, but it give the following error.
Fatal error: Call to undefined function base_url() in
C:\wamp\www\Test-CI\application\views\layout.php on line 5
 
{main}( ) IN ..\index.php:0 require_once('C:\wamp\www\Test-CI\system\core\CodeIgniter.php' ) IN ..\index.php:202
call_user_func_array ( ) IN ..\CodeIgniter.php:359
Home->index( ) IN ..\CodeIgniter.php:0
CI_Loader->view( ) IN ..\home.php:17
CI_Loader->_ci_load( ) IN ..\Loader.php:419
include('C:\wamp\www\Test-CI\application\views\layout.php' ) IN ..\Loader.php:833
My code :
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Galleriffic | Custom layout with external controls</title>
<link rel="stylesheet" href="<?php base_url(); ?>/assets/css/basic.css" type="text/css" />
<link rel="stylesheet" href="<?php base_url(); ?>/assets/css/galleriffic-5.css" type="text/css" />
<!-- <link rel="stylesheet" href="<?php base_url(); ?>/assets/css/white.css" type="text/css" /> -->
<link rel="stylesheet" href="<?php base_url(); ?>/assets/css/black.css" type="text/css" />
<script type="text/javascript" src="<?php base_url(); ?>/assets/js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="<?php base_url(); ?>/assets/js/jquery.history.js"></script>
<script type="text/javascript" src="<?php base_url(); ?>/assets/js/jquery.galleriffic.js"></script>
<script type="text/javascript" src="<?php base_url(); ?>/assets/js/jquery.opacityrollover.js"></script>
<!-- We only want the thunbnails to display when javascript is disabled -->
<script type="text/javascript">
document.write('<style>.noscript { display: none; }</style>');
</script>
</head>
You have to load the url helper to access that function.
Either you add
$this->load->helper('url');
somewhere in your controller.
Alternately, to have it be loaded automatically everywhere, make sure the line in application/config/autoload.php that looks like
$autoload['helper'] = array('url');
has 'url' in that array (as shown above).
just add
$autoload['helper'] = array('url');
in autoload.php in your config file
You need to load the helper before loading the view somewhere in your controller.
But I think here you want to use the function site_url()
Before you load your view, basically anywhere inside the method in your controller, add this to your code :
$this->load->helper('url');
Then use the function site_url().
There is only three way to solve.
paste in following code in this path application/config/autoload.php.
most of them I use this
require_once BASEPATH . '/helpers/url_helper.php';
this is good but some time, it fail
$autoload['helper'] = array('url');
I never use it but I know it work
$this->load->helper('url');
To load other file(css ,js) and folder(images) , in codeigniter project
we have to follow two simple step:
1.create folder public (can give any name) in your codeigniters folder put your bootstrap files css and js folder in side public folder. https://i.stack.imgur.com/O2gr6.jpg
2.now we have to use two line of code
1.load->helper('url'); ?>
2.
go to your view file
put first line to load base URL
<?php $this->load->helper('url'); ?>
secondly in your html head tag put this
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>/public/css/bootstrap.css">
now you can enjoy the bootstrap in your codeigniter
you have to use echo before base_url() function. otherwise it woudn't print the base url.
Go to application/config/autoload.php
$autoload['helper'] = array('url');
add this on top anywhere
and at this in controller
function __construct()
{
parent::__construct();
$this->load->helper('url');
}
codeigniter needs to load the url helper to call the base_url() function.
use application->config->autoload.php
edit autoload helper array and add url helper..
base_url() function is working now..
first you have to give echo to display base url. Then change below value in your autoload.php which will be inside your application/config/ folder.
$autoload['helper'] = array('url');
then your issue will be resolved.
Simply add $autoload['helper'] = array('url'); to autoload.php.
Check that you are extending CI_Controller class in your controller and in the view you have to echo base_url($path) function, otherwise it will not work.
Just create a variable as $base_url
$base_url = load_class('Config')->config['base_url'];
<?php echo $base_url ?>
and call it in your code..
..
here i set value of href is the path of file you get this path by following step
search localhost on browser then go in directory where file saved & then copy url
note* ** if you have any index.php or index.html or other index named file change the name temporary for get url then after you undo file name **
use this not need use function
<link href="http://localhost/CodeIgniter-2.2.6/css/sm_reg.css" rel="stylesheet">
here i set value of href is the path of file you get this path by following step
search localhost on browser then go in directory where file saved & then copy url
note* ** if you have any index.php or index.html or other index named file change the name temporary for get url then after you undo file name **
use this not need use function

Categories