OllyBarca
OllyBarca

Reputation: 1531

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.

Upvotes: 0

Views: 950

Answers (2)

Alice
Alice

Reputation: 144

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

Upvotes: 1

Masoud
Masoud

Reputation: 1351

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

Upvotes: 1

Related Questions