How to deal with routing when developing a custom CMS in Codeigniter - php

I’m a recent user of Codeigniter and am developing a simple backend CMS to manage pages.
Based on a URL (in this example I have hidden “index.php”) : mysite.com/pagename
I would like the system to detect if there is a value of “pagename” in my database, if there is, I need the system to re-route to a custom controller (eg: Pagemaker) and if there is no record called pagename, just do it’s normal thing (i.e. find a controller called pagename)
Currently I have:
$route['(:any)'] = "pagemaker/create/$1";
whereby all requests are forwarded to my custom function.
However I want to change this structure so that if the page does NOT exist in the db, the traditional codeigniter request process is followed.
Can anyone offer any advice about how to complete this? Or any advice about routing custom CMS’s in codeigniter in general?

The best solution is to upgrade to CI 2.0 because it's stable enough and it gives you plenty of useful features.
In your case, set the following route:
$route['404_override'] = 'pagemaker';
If the router doesn't know where to go it just goes to pagemaker controller. This can then check if the first uri segment exists and if not you create a custom 404 page instead of the crappy default one.
And I don't want to hear any of this "Oh but it's not released yet" crap, I've been using it CI 2.0 for almost a year. ;-)

I can think of two possibilities:
1) Edit your custom function to let it redirect your client when page's not in the db
pseudo code:
if($dbresult == null){
redirect("http://yoursite.com/"+$this->uri->segment(3));
}
2) Edit the router class of CI so it will first check if the page's in the db and if not, just continues. This may be somewhat messier as you need a db connection in your Router.php

Related

Routing with laravel

So I am working on a project and I have decided to use php laravel framework, but when it comes to things like creating a new page and dealing with page redirects etc, am I right in thinking that all these will be handled in the route/web.php file so all the pages for my application will be defined in the route along with the view ?
I was thinking what if my application grows to have dozens of pages is it best practice to define each one on the route or are there better ways to handle this?
Yes, the best way to do routing is laravel to have each route for . each page, the only exception is when you have dynamic routes, for example, if you have a route that checks for users id or category for some product etc and it looks something like this Route::get("/product/{$category_id}","FrontController#methodForGetingProduct").
And later in Controller, you define what will you send and receive of the information and what view should be returned.
you can follow this type routing .......

Mini CMS - need some advices on pages

I am going to write a super small cms with https://github.com/panique/mini/
Now I want to add a small pages section in the Admin of the site (this can be done easily ).
The advice part comes here:
The url of the mini framework is mostly easy, its /controller/method ( if its the index method then it won't needed to be shown in the url ).
So there is a file which checks if the controller is existing so it can load it.
But the thing is an user is not going to create a controller every time after creating a page.
What would be the best approach to do this ?
This file is checking if that controller exist: https://github.com/panique/mini/blob/master/application/core/application.php
Thanks in advance
The way this handles routing, is much like CodeIgniter. The path would be controller/method/arg1/arg2/arg3..., so you can write a controller, define a method which accepts one (or more) arguments to load user pages.
Assume, user has created a page named news. He/She may load the page via URL pages/view/news.

Codeigniter Dynamic Routing to single controller

I know this question has been asked in a similar fashion several times. However, I'm struggling to find any answers that would work in my situation. I primarily work on Microsoft projects and stepped in on this project to help during crunch.
Here's the situation.
We have a client who has a site with over 600 different pages. In reality each page uses the same template just populates with different data. We've developed a CMS for him which allows him to create new pages at will.
My ideal solution would allow me to store the name of a newly created page in a DB.
Ex. new_page_1 has been created and now exists in the DB. Now when I type in www.mysite.com/new_page_1 this needs to go to a controller that looks up "new_page_1" (in DB) and if it exists loads a view (THIS VIEW WILL NEED TO BE USED FOR ALL 600 pages) which then takes other data from the DB and populates various sections.
So essentially, over 600+ pages need to use the same route array and map to the same controller which then maps to the same view.
I've tried using $route['(:any)'] = 'custom_controler/create/$1 and as well as the same array key but using main and _remap. No matter what every single time it tries to look for the page name in my views (which it will never exist because I'm using one generic view for 600 pages)
Any ideas on how to accomplish this?
UPDATE
routes.php (this is the last line in the file)
$route['(:any)'] = "main/create/$1";
main.php (controller)
class Main extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function create($page)
{
$c = new Category();
$c->get_by_name(ucfirst($page));
$this->load->view('site/index',$c);
}
}
the URL I'm attempting is sitename.servername.com/health sitename and servername obviously substituted.
The error I get is
An Error Was Encountered
Unable to load the requested file: health/main/create.php
Is the error page you're seeing the CodeIgniter error template or a generic server error? That error string sounds very much like you are using Apache or Nginx (or whatever main webserver you use) and its actually not even resolving to your CodeIgniter app at all, but searching for a PHP file that doesn't exist. You'll probably need to use mod_rewrite or something like that to make that URL point at the CodeIgniter install.
Otherwise, your implementation doesn't look completely wrong: you probably need to make sure main.php is the default route as well.
Your application will only look for the page name in your views if you tell it to. Your catch-all controller method should be checking the database for a valid page name, and then regardless of which pages validate, loads the same view (albeit with different data passed to the view).
Using an (:any) catch-all route is perfectly fine. Your controller code somewhere is what's throwing you off. Update your post with the code if you continue to struggle.

run a function on each page

My codeigniter app is multilingual and I want to redirect users to their pages, by checking IP address.
I should check it at the top of all pages (i know i can set a session or cookie, but i want to check it on all pages); before any views or other.
Where should i put my code (function)? on Startup file or Loader? or create an extension or plugin and load it on Startup? if it can be done by an extension or plugin, how can I create it? (i've searched, but didn't find a useful tutorial)
Thanks.
If you're using a main front controller you can put your code in there. But a better way to do it would be to use CodeIgniters built in functionality to extend the core - hooks!
http://codeigniter.com/user_guide/general/hooks.html
Just select the point you want your script to be activated and take it from there.

How to make a correct Joomla URL using the Joomla Classes

I am busy making a Joomla! component. And know i want to link to another component. So, as example i want to link from my component 'my_component' to 'his_component'. Normally this is simple to do. Just make a normal link like href="?option=com_his_component". But the problem is that one of my consumers uses SEO friendly URLs. And in that case this URL won't work.
Does anyone know a way to this the correct way (I think with the Joomla! Api)
Just wrap that link with a call to JRoute::_($url):
$link = JRoute::_('index.php?option=com_his_component&foo=bar');

Categories