Welcome Guest, Not a member yet? Register   Sign In
Database Driven Routes without hooking or datamapper [UPDATED]
#11

[eluser]codeninja[/eluser]
For some reason I have to manually clear the cache to get the newly added routes enabled. Is there something I can do to make this better?

Also, how about the order of the routes?

eg.

$routes['xyz/(:any)'] = "controller/somemethod"
$routes['xyz/something'] = "new_controller/someothermethod"

in this case i want to make sure that if you call xyz/something then it check it in that order but since that is the order they are entered in the db how can I ensure correct order?

Thanks
#12

[eluser]Rolly1971[/eluser]
you need to put it like this:

$routes[‘xyz/something’] = “new_controller/someothermethod”
$routes[‘xyz/(:any)’] = “controller/somemethod”


for it to work. The way you have your routes listed $routes[‘xyz/(:any)’] is super-ceding any route listed after it that would start with 'xyz'

so it would take xyz/something and instead of routing it to 'new_controller/someothermethod' it sends it 'controller/somemethod'
#13

[eluser]codeninja[/eluser]
Rolly1971: I agree that that's how i need to put it but if i had entered xyz/(:any) in my db first and then a developer comes along and adds xyz/something than what order would the routes.cache have them in?
#14

[eluser]Rolly1971[/eluser]
well you may need to alter the code to test for 'xyz/(:any)' in the route, and resort the array returned in order to ensure it is not overriding other routes.

personally, though, i do not use db driven routes. i use the CI routes file with generic route defs to determine what needs to be loaded for content from the db. Using the controllers index function to test the uri.

in my case, for the front end site, the first segment of the uri is the page that needs loading.

eg:

example.com/about, example.com/register, example.com/login all get routed to the welcome controller. the index function reads the uri segment and loads the content from the db based on the uri string.

when a new page is added to the database in the admin, a new menu item is also created in the db, which is then loaded into the page on next page load. With the 'name' field being used as the uri segment.

this probably does not really help our situation though, but like i said, you may need to alter the MY_Router to do checks for (:any) in the route and resort the array to ensure they do get put in the right spot as not to override end user defined routes.
#15

[eluser]WanWizard[/eluser]
You might want to look at Modular CI.

It supports loading secondary controllers (HMVC style). It includes a routing controller, which by default only has module routing capabilities, but as a controller it has full access to CI resources. It can access the database to retrieve detailed routing information, and then load the controller the loaded route points to.
#16

[eluser]m4ikel[/eluser]
Please review the following solution which enables you to use the default database class inside the Router.php file and tell me if this works for you:

http://ellislab.com/forums/viewthread/177130/#840162
#17

[eluser]ranjudsokomora[/eluser]
Good Day CI Coders,
Sorry I haven't gotten a chance to respond to this thread for a while.

codeninja,
The easiest way to set the order of the routes is to an integer column to the database and order all the results by the integer column.

CI_Maikel,
You can use the Database class in the router class by simply adding
Code:
REQUIRE_ONCE(BASEPATH."database/DB.php");
$DB =&DB;("default",FALSE);

I don't understand why you would need to edit ./system/codeigniter/CodeIgniter.php
Not that I am against it, I just find no need to do that.
#18

[eluser]zvir[/eluser]
[quote author="ranjudsokomora" date="1275510767"]Hi Addow,
I liked your suggestion of cached results. So here is that version! With the database class right in the Router class Smile In any case here are the instructions:

1) Download the zip file attached to this post
2) Extract both files to the ./system/application/libraries folder
3) Modify your ./system/application/config/database.php to reflect your database connection settings


SIDE NOTE: If your routes table is in a database other than "default" you will need to add this to your ./system/application/config/database.php file.
Code:
$db['router']['hostname'] = "localhost";
$db['router']['username'] = "";
$db['router']['password'] = "";
$db['router']['database'] = "";
$db['router']['dbdriver'] = "";
$db['router']['dbprefix'] = "";
$db['router']['pconnect'] = TRUE;
$db['router']['db_debug'] = TRUE;
$db['router']['cache_on'] = FALSE;
$db['router']['cachedir'] = "";
$db['router']['char_set'] = "utf8";
$db['router']['dbcollat'] = "utf8_general_ci";

Of course filling in your information.

Here is my SQL table
Code:
CREATE TABLE `tblRoutes` (
  `routeID` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `vchRoute` VARCHAR(150) NOT NULL,
  `vchController` VARCHAR(150) NOT NULL,
  `dteEntryDate` DATETIME NOT NULL,
  `dteModifyDate` DATETIME NOT NULL,
  PRIMARY KEY (`routeID`)
)

This extended router class is very customizable and I tried to make easy to edit things.
Let me know how I did.

Thanks[/quote]

can't download attachment!!




Theme © iAndrew 2016 - Forum software by © MyBB