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

[eluser]fesweb[/eluser]
If you really wanted to you could forget about the routes altogether...

In your root level controller (or your Pages controller):
Code:
function _remap($method)
    {
        if (method_exists($this, $method))
        {
            $this->$method();
        }
        else
        {
            $this->static_page_handler();
        }
    }

function an_existing_method()
{
  // do stuff
}
function static_page_handler()
{
    $this->page_file_name = APPPATH.'views/pages/'.$this->uri->rsegment(3).'.php';        
    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.');
        }
}
#12

[eluser]kyleect[/eluser]
[quote author="fesweb" date="1233611729"]If you really wanted to you could forget about the routes altogether...

In your root level controller (or your Pages controller):
Code:
function _remap($method)
    {
        if (method_exists($this, $method))
        {
            $this->$method();
        }
        else
        {
            $this->static_page_handler();
        }
    }

function an_existing_method()
{
  // do stuff
}
function static_page_handler()
{
    $this->page_file_name = APPPATH.'views/pages/'.$this->uri->rsegment(3).'.php';        
    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.');
        }
}
[/quote]

I still need routes as method_exists only works on Pages and if I have a link http://localhost/static_page, the page controller isn't being called until I route http://localhost/static_page to http://localhost/pages/static_page. I would love to get rid of the routes if I could.
#13

[eluser]Colin Williams[/eluser]
Quote:I would love to get rid of the routes if I could.

That's what TooPixel's setup does.
#14

[eluser]Craig A Rodway[/eluser]
[quote author="kyleect" date="1233347179"]2) Controller takes segment 2 of the pages/static_page url and loads the correct view:
[/quote]

In your route, I believe the segment you want is 1. Thus:

Code:
<?php
class Pages extends Controller {

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

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

As segment 2 doesn't exist, CI is looking for pages/{NOTHING}.php

Either that, or stick it in an index() function and change your routes file accordingly.
#15

[eluser]kyleect[/eluser]
Yea I caught that too haha. Here is finished version in case anyone is interested. Like I said it still requires routes but I don't know how to get around that:

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

/**
* Pages
*
* Displays static views not associated with another controller.
*
* Routing is still required for this: http://localhost/static_page
* The routing will look like this:
*      $routes['static_page'] = 'pages/static_page';
*
* The views for these static pages are located: Application/Views/Pages
*
* TODO for version 0.02:
*      - Figure out how to rid the requirements of routes
*      - add support for directories indicated by url segments
*
* @package CI1.7_kyct
* @subpackage controllers
* @author Kylee Tilley <[email protected]>
* @version 0.01
*/

// ------------------------------------------------------------------------

/**
* Controller: Pages
*
* Displays static views not associated with another controller.
*
* @package CI1.7_kyct
* @subpackage controllers
**/

    class Pages extends Controller {
    
        /**
         * name
         *
         * The name of the view being loaded
         *
         * @var string
         **/
        
        private $name;
        
        /**
         * path
         *
         * The server path to the view file being loaded
         *
         * @var string
         **/
        
        private $path;
        
        /**
         * view
         *
         * The segment path of the view being loaded
         *
         * @var string
         **/
        
        private $view;

        function __construct()
        {
            parent::__construct();
        
            $this->name = $this->uri->segment(1);
            $this->path = APPPATH.'views/pages/'.$this->name.'.php';
            $this->view = 'pages/'.$this->name;
        }
        
        /**
         * _remap
         *
         * _remap overrides normal MVC behavior by not automatically running the method being requested.
         * This is being used to avoid needing a method to display the pages.
         *
         * @return void
         **/
        
        function _remap($method)
        {
            if(method_exists($this, $method))
            {
                log_message('debug', 'Controller: Pages: _remap: Method "'.$method.'" is defined. Overwriting default behavior.');
                $this->$method();
            }
            else
            {
                if(file_exists($this->path))
                {
                    log_message('debug', 'Controller: Pages: _remap: "'.$this->path.'" found. Loading view: "'.$this->view.'"');
                    $this->load->view($this->view);
                }
                else
                {
                    log_message('error', 'Controller: Pages: _remap: "'.$this->path.'" not found. but file could not be located. Sending error code 404 to the browser.');
                    show_404();
                }
            }    
        }
    }
?&gt;

Also, an unrelated question, does anyone know if I can declare more than one @subpackage in a phpdoc block? The official documentation isn't the best.
#16

[eluser]iDenta[/eluser]
I wanted to try TooPixel's solution, but i can't get it to work...

I added the MY_Routes.php library and the page.php controller, and the created the "pages" folder in my views folder. But when i try to access a page i just get "Unable to load the requested file: about.php"

Also, when i go to a page that doesn't exist i the "pages" folder like "www.domain.com/abouteee", i get the error "Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid." but that seems strange when there is no problem loading the default controller in other situations.

Have i missed something?
#17

[eluser]Référencement Google[/eluser]
In your view folder, is it the about.php page ?
What I've posted is a way of doing it, that was not the full MY_Routes.php. Here is below the full class I am using:

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

/**
* Router Class
*
* Extends CI Router
*
* @author     Original by EllisLab - extension by Too Pixel
* @see        http://codeigniter.com
*/

class MY_Router extends CI_Router {

    /**
     * Constructor
     *
     * @access    public
     */
    function MY_Router()
    {
        parent::CI_Router();
    }

    // --------------------------------------------------------------------

    /**
     * Validate Routing Request
     *
     * @access    public
     */
    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;
        }

        // Check the root folder first
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Not in the root, but not enough segments
        if (count($segments) < 2)
        {
            //Calling the index function of a controller of the same directory...
            //We'll cheat and just set our segment
            $segments[1] = $segments[0];
        }

        // Does the requested controller exist as a full path including the directory?
        if (file_exists(APPPATH.'controllers/'.$segments[0].'/'.$segments[1].EXT))
        {
            //Set the directory
            $this->set_directory($segments[0]);

            //Drop the directory segment
            $segments = array_slice($segments, 1);
            return $segments;
        }

        //Ok, that didn't work, let's try duplicating segment 0, maybe it's the same ;).
        if (file_exists(APPPATH.'controllers/'.$segments[0].'/'.$segments[0].EXT))
        {
            //Set the directory
            $this->set_directory($segments[0]);

            //We cheated so there's nothing to drop
            return $segments;
        }

        // Can't find the requested controller...go to homepage
        return array('page', 'index');
    }

}
// END MY_Router class

/* End of file MY_Router.php */
/* Location: ./application/libraries/MY_Router.php */
#18

[eluser]iDenta[/eluser]
Yep, the page is about.php, just a simple testpage to see if it's working.

I added the entire class to MY_Router.php, but it still doesn't work, same error.

Can it have something to do with my config/routes.php file? Although it's pretty much original, just added
one extra route "$route['company/:any'] = "company/show";"
#19

[eluser]Référencement Google[/eluser]
Did you set your default route to an existing controller ?
#20

[eluser]iDenta[/eluser]
yep, $route['default_controller'] = "main"; and it seems to work as it should.

I also removed my own route, didn't make any difference... Other than that my CI installation is pretty much standard, i have a .htaccess to remove index.php and is using dx_auth.




Theme © iAndrew 2016 - Forum software by © MyBB