ezrpg-legacy / ezrpg-2.0.X-discontinued-

http://ezrpgproject.net
Other
6 stars 0 forks source link

Router #4

Closed ghost closed 11 years ago

ghost commented 11 years ago

What do you say about implementing a simple Router which allows user to set custom routes, i.e. having a StaticController with pages like About, etc than you probably would want to route

/about = StaticController/AboutAction. etc.

This wouldn't be very hard to implement, just make sure to return the same data given from the current Routing system.

ferdis commented 11 years ago

You can do a prototype if you want to.

I'm not against this idea at all, but a router requires a dispatch chain, which requires a dispatcher and in turn request and response classes. Maybe just a router that hooks into a PSR-0 autoloader? That could potentially work and still keep it lightweight.

uaktags commented 11 years ago

Yea by all means try the idea out, just keep the idea in mind to make as powerful and lightweight as possible.

uaktags commented 11 years ago

Now I don't want to seem like an idiot with this suggestion, so forgive me for ignorance, but I was wondering today, what if we made a check go into where the controllerName = 'error404' in ezRPG\App to check a Route folder for our routes?

// Extract controller name, view name, action name and arguments from URL
        if ( !empty($_GET['q']) ) {
            $this->args = explode('/', $_GET['q']);

            if ( $this->args ) {
                $this->controllerName = str_replace(' ', '/', ucwords(str_replace('_', ' ', str_replace('-', '', array_shift($this->args)))));
            }

            if ( $action = $this->args ? array_shift($this->args) : '' ) {
                $this->action = str_replace('-', '', $action);
            }
        }

        if ( !is_file('ezRPG/Controllers/' . $this->controllerName . '.php') ) {
            $this->controllerName .= '/Index';

            if ( !is_file('ezRPG/Controllers/' . $this->controllerName . '.php') ) {
                $this->controllerName = 'Error404';
            }
        }

so something like:

// Extract controller name, view name, action name and arguments from URL
        if ( !empty($_GET['q']) ) {
            $this->args = explode('/', $_GET['q']);

            if ( $this->args ) {
                $this->controllerName = str_replace(' ', '/', ucwords(str_replace('_', ' ', str_replace('-', '', array_shift($this->args)))));
            }

            if ( $action = $this->args ? array_shift($this->args) : '' ) {
                $this->action = str_replace('-', '', $action);
            }
        }

        if ( !is_file('ezRPG/Controllers/' . $this->controllerName . '.php') ) {
                        if( $this->checkRouter($this->controllerName) === false ){
                                $this->controllerName .= '/Index';
                          }     

            if ( !is_file('ezRPG/Controllers/' . $this->controllerName . '.php') ) {
                              $this->controllerName = 'Error404';
            }
        }

That way, if I have this correctly, we can make a function in the App.php file called checkRouter($controllerName) which will then parse the Router file for a matching record OR it can even be DB base (idk if that's helpful or not) but basically it would help set a matching keyword for records. The routing is already in place, theres just nothing here for custom entries. This small addition should do the trick.

uaktags commented 11 years ago

https://github.com/Th3-Night/Routy may also be worth a look, but thats enough from me. I honestly have little experience with the inner dealings of routing, always just been a user with them rather a developer.

ferdis commented 11 years ago

That router seems very JavaScript-like. I'm not a big fan of lamba callbacks such as that library uses.

My idea of a router for ezRPG is simply an autoloader, with injection capabilities. Some PSR-0 Autoloaders, like the one I've created, have something called resolvers. These are key-value pairs that helps the autoloader resolve a URI to a class.

When ezRPG supports modules again, and they are database driven, we can implement a DB driven router system as well, but for now just scanning over the modules directory will be sufficient to populate default routes. The only problem I for-see is that routes can't be abstracted, such as just being a template.

On Fri, May 17, 2013 at 3:25 AM, uaktags notifications@github.com wrote:

https://github.com/Th3-Night/Routy may also be worth a look, but thats enough from me. I honestly have little experience with the inner dealings of routing, always just been a user with them rather a developer.

— Reply to this email directly or view it on GitHubhttps://github.com/uaktags/Rework-Revisited/issues/4#issuecomment-18039126 .

uaktags commented 11 years ago

SCANDIR_SORT_NONE (ezRPG\Router\Line70) is undefined until php5.4 so trying to get my wamp to update past 5.3.9 for it to test.

Okay, so somethings with the update to keep in mind, and i'll do some of the fixes in a min, but casing counts now for the url. so index must be Index like when the Login function succeeds, the header must go to Index now instead of index else a 404 will be displayed. URLs I had set to "index" or "login" instead of "/index" or "/login" due to if you're using a subdirectory for your install "/index" then resolves to "localhost/index" rather than "localhost/ez/index". While this clearly isn't an issue for a production install when a game will (should?) be installed in root dir, perhaps a config var should be declared just in case so all urls can be affixed with the correct directory and we can make sure all birds are hit with the same stone. Also, just confirm, my "Hello, kitty" surprise is throwing a 404 on "Index/hello"?

uaktags commented 11 years ago

I'm not sure if it's just me but the router seems to not be working in regards to the "Hello Kitty" example in Index/hello

ferdis commented 11 years ago

I'll add that to my list as well. The constant for scandir will also need to be changed or removed, I haven't looked at the PHP changelog but I think that scandir SORT_NONE shoyld have been defined before 5.4 already.

It wouldn't really matter anyway to remove it though, it's just there for optimization and file priority. — Sincerely Ferdi Schmidt

On Fri, May 24, 2013 at 5:24 PM, uaktags notifications@github.com wrote:

I'm not sure if it's just me but the router seems to not be working in regards to the "Hello Kitty" example in Index/hello

Reply to this email directly or view it on GitHub: https://github.com/uaktags/Rework-Revisited/issues/4#issuecomment-18411541

uaktags commented 11 years ago

Router now works for me.