Link to static page in CodeIgniter

5.7k Views Asked by At

I'm new to PHP and CodeIgniter, and I'm having trouble setting up the routing of static pages in my CodeIgniter app.

I have the About Us | Contact Us | Privacy Policy and many other pages in my footer, which when the user clicks, should go to a url like mysite.com\about mysite.com\contact mysite.com\privacy

You get the gist..

Here's what I've done in the footer div in the views folder

<a href="home/staticpages/about">About</a> |
<a href="home/staticpages/contact"> Contact Us</a> | 
<a href="home/staticpages">Privacy</a> | 
<a href="home/staticpages">Terms of Use </a>

Here's the function in a controller Home

public function staticpages($page)
    {

        if ( ! file_exists(APPPATH.'application/views/'.$page.'.html'))
        {
            // Whoops, we don't have a page for that!
            show_404();
        }

        $data['title'] = ucfirst($page); // Capitalize the first letter

        $this->load->view($page, $data);

   }

The static pages are just pure HTML content, with <p> fields and no dynamic or javascript stuff. So I have them saved as .html; contact.html, about.html and so on. I just want to load those pages' content in a contentbody div I have created, between the header and the footer, and that's the only thing that changes in the whole site for whatever the user clicks.

When I goto http://localhost/myapp/home/staticpages/about

I get the

404 Not Found error The requested URL /dissekted/home/staticpages/about was not found on this server.

And it's the one that the browser throws.

3

There are 3 best solutions below

0
On

Check your base URL in the config file and make sure you have the final / on the path:

$config['base_url'] = 'http://localhost/myapp/'

Also, I use the anchor() function from the URL Helper to take create my links.

That takes care of forming the links correctly and takes into account the base URL.

0
On
create privacy.php in view folder and use below code to display it.


$this->load->view('page_name'); no need to use extension like static.php.

function privacy() {`enter code here`
        $data['meta_title'] = 'Privacy Policy';
        $data['title'] = 'Privacy Policy';
        $data['main'] = 'root/privacy';
        $this->load->vars($data);
        $this->load->view($this->template or page_name);
}
2
On

config your base url

$config['base_url'] = 

load the helper

first method

   $this->load->helper('url');

or second method autoload it by changing application/config/autoload.php

use with

$this->config->base_url();

first check with echo you are getting base url or not

<?php echo base_url(); ?>

and use with your static link

<a href="<?php echo base_url(); ?>/static_file "  />static link</a>

ok make this inside in controllers function like

public function static ()
    { 

    $this->load->view('static.html');
}