Welcome Guest, Not a member yet? Register   Sign In
Creating a controller for serving static pages? *SOLVED*
#1

[eluser]kyleect[/eluser]
There are some pages in my projects that are static and can stand alone. I decided to create a controller to serve up these pages using clean routed urls. Here is how it should work:

1) Route the clean version of the static page url. Example:

$routes['static_page'] = 'pages/static_page';

2) Controller takes segment 2 of the pages/static_page url and loads the correct view:

Code:
<?php
class Pages extends Controller {

    function Pages()
    {
        parent::Controller();
        $this->load->view('pages/'.$this->uri->segment(2));
    }    
}
?>

This isn't working however. here is how my views folder is set up:

Quote:views/
pages/
static_page.php

static_page.php has "hello world" written in it. However when I go to http://localhost/why_register it outputs an error:

Quote:An Error Was Encountered
Unable to load the requested file: pages/.php

Does anyone know how to make this work? What I need to change?
#2

[eluser]kyleect[/eluser]
Found a way but not 100% happy with it. Hell, it works though:

Code:
$route['static_page'] = 'pages/view/static_page';

Code:
<?php
class Pages extends Controller {

    function Pages()
    {
        parent::Controller();
    }
    
    function view()
    {
        $this->load->view('pages/'.$this->uri->rsegment(3));
    }
}
?>
#3

[eluser]fesweb[/eluser]
Simple solution first: Couldn't you just change the name of your function to index() and get rid of the view part of the url?
#4

[eluser]Référencement Google[/eluser]
Here is a solution I came about using a custom MY_Router.php :

./application/libraries/MY_Router.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Router extends CI_Router {

    function MY_Router()
    {
        parent::CI_Router();
    }

    function _validate_request($segments)
    {
        // Check for a page to load in views/pages/ folder
        if (count($segments) == 1 AND file_exists(APPPATH.'views/pages/'.$segments[0].EXT))
        {
            array_unshift($segments, 'page', 'show');
        }

    return $segments;

    }

}

./application/controllers/page.php
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Page extends Controller {

    function Page()
    {
        parent::Controller();
    }

    function index()
    {
        $this->show();
    }

    function show($url = 'index')
    {
        if( ! file_exists(APPPATH.'views/pages/'.$url.EXT))
        {
            show_404();
        }

                $this->load->view($url);
    }

}
#5

[eluser]kyleect[/eluser]
Quote:Simple solution first: Couldn’t you just change the name of your function to index() and get rid of the view part of the url?

No, I tried that, it tries to read the page name as the action segment of the url. Creating the view action stopped that. It's not a huge deal as people will never see pages/view/static_page in the url, just had having to have the view action. haha It's my own anal retentive coding issues at work.
#6

[eluser]Référencement Google[/eluser]
I don^t know if you get it, but my solution will let you fetch your static pages typing www.yoursite.com/about for example (where about is in views/pages/about.php)
#7

[eluser]kyleect[/eluser]
Mine does as well

http://localhost/static_page

loads

views/pages/static_page

The user never sees http://localhost/pages/view/static_page, they only see http://localhost/static_page. Mine just doesn't have checks to see if the file exists yet.
#8

[eluser]Référencement Google[/eluser]
So if your solution work, why you've posted it saying it wasn't working then says you wasn't happy about it ?
#9

[eluser]kyleect[/eluser]
I started this post because what I was trying wasn't working. I posted my solution incase others wanted it and also if someone could suggest a better way. Yours was almost the same but had some things that wouldn't be needed. The pages controller is only for finding and displaying single static pages. The index method wouldn't be needed at all and your show action was the same as my view action with file exist checking. I was trying to find a way to eliminate the need for the show/view action all together. I'm working on something using the __call() magic method but haven't tested it. I will also post that when it's working because it's all about community.
#10

[eluser]kyleect[/eluser]
Just tested this and it works exactly the same as my second post but removing the view action. So in routes you just have to put "pages/static_page" and it works.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pages extends Controller {
    
    private $page_file_name;

    function Pages()
    {
        parent::Controller();
        $this->page_file_name = APPPATH.'views/pages/'.$this->uri->rsegment(3).'.php';
    }
    
    function _remap($method)
    {
        if(file_exists($this->page_file_name))
        {
            $this->load->view('pages/'.$this->uri->rsegment(3));
        }
        else
        {
            show_404();
            log_message('error', 'Trying to load "'.$this->page_file_name.'" but file could not be located. Sending error code 404 to the browser.');
        }
    }
}
?>




Theme © iAndrew 2016 - Forum software by © MyBB