Creating a controller for serving static pages? *SOLVED* |
[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 This isn't working however. here is how my views folder is set up: Quote:views/ 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 Does anyone know how to make this work? What I need to change?
[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
[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?
[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'); ./application/controllers/page.php Code: <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
[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.
[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)
[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.
[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 ?
[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.
[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'); |
Welcome Guest, Not a member yet? Register Sign In |