CodeIgniter Forums
Routes config from within a library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Routes config from within a library (/showthread.php?tid=3347)



Routes config from within a library - El Forum - 09-25-2007

[eluser]easymind[/eluser]
Is it possible to config routing from within a home made library? For example {site}/login and {site}/logout. I don't want to make separate controllers for these urls because I want a 1 file lib for easy distribution.


Routes config from within a library - El Forum - 09-25-2007

[eluser]Michael Wales[/eluser]
You would make a route in routes.php that would point to whatever controller/method you want.

For example:
Code:
$route['login'] = 'user/login';
$route['logout'] = 'user/logout';

Code:
class User extends Controller {

  function login() {
    // Do the login
  }

  function logout() {
    // Do the logout
  }

}

Note: That array may be wrong... not looking at the user guide or code - but you get the point.


Routes config from within a library - El Forum - 09-25-2007

[eluser]easymind[/eluser]
Ok, this is not what I mean. This is the 'normal' way and it requires me to make a controler and edit my routes.php. I want to avoid both. I want something like:

Code:
$data = array(
'login'=>'welcome',
'logout'=>'welcome'
}
$CI->config->routes($data);

This should be done in my lib. This way I can copy my lib to other pojects and not worry about creating controlers or adjusting the routing.php

Understand?

I know it is not good MVC practice. And libs should only provide functionality and I should implement this functionality using controlers. But I don't really care and what this 1 lib file solution....


Routes config from within a library - El Forum - 09-25-2007

[eluser]Michael Wales[/eluser]
* El Forum shrugs.

I've never seen anyone do it like this (or request it) - anything is possible though.


Routes config from within a library - El Forum - 09-25-2007

[eluser]easymind[/eluser]
Ah forget it. I think I just thought of a work around...


Routes config from within a library - El Forum - 09-25-2007

[eluser]zwippie[/eluser]
About your first question, routing done by a library:

I don't think it is possible, because the routes are parsed and checked against the URI before the controller gets loaded. Autoloading of libraries is performed by the controller, so its too late then to reroute the request.
I might be wrong here, but I think this is what's happening when I try to follow execution flow of system/codeigniter/CodeIgniter.php.

All this is aside from the point that a one-in-all library file (containing routes, configs, views and the more) does not really fits the CI philosophy. Don't let a couple of more files or config rules scare you or you're future library users. ;)



About your last question:
(edit: the question you just removed Wink )

When an unauthenticated request is done, perhaps you can store the request URI in the usersession, redirect to perform the login, when that is ok redirect to the stored URI.


Routes config from within a library - El Forum - 09-25-2007

[eluser]easymind[/eluser]
Your solutions do not solve my problem. I can do it by the book, but I don't want that. My original site's files can be controllers and views. But I like my lib to contain all that is needed for the lib.

I thought I needed to route urls to fix my problem but I found another solution for my lib that needs no extra routing or controllers.

If, for example, I want to show the 'requestpasswordform' now, I don't have an url (routing or controller) that sets a session var to show the requestform, then redirects back and shows it. But now I just POST a variable like loginform_reqpass=true to any controller. I catch the post vars in my lib and can set the lib to show the right form.

These forms appear as small login/requestpass/logout forms just below my main menu, so the maincontent should not change when people are messing with the loginforms.

I think I will try this way more. Maybe to set language and stuff. I usualy routed or made controllers for this too.