Route everything to index.php

Hi,

I'm building a CMS of sorts, with an undefined number of unique URLs. I'd like all URLs to be redirected to my Index controller. What do I need to make my route in order for this to work?

I've tried:

'/*'  =>   [   'controller'  =>   'index',
                'action'      =>   'index']

But that doesn't work. I know I could probably just route all 404 traffic to index, but that seems dirty.



44.7k
edited Feb '15
$router->add('/([a-zA-Z0-9]+)/:params', [
    'module'      => 'main',
    'controller'  => 'index',
    'action'      => 'index',
    'article'     => 1,
    'params'      => 2
]);

Then try improving that regex with some research on the best UTF8 printable stuff minus the URL specific stuff.

With remove trailing slashes of course.

[edit] I edited.

That didn't work, but this regex did: /(.*)

I'm confused as to why I didn't try that before posting.



44.7k

That is ok but what if you get URLs like /Title/edit ?

The URLs aren't intended to point to a controller or action - they are simply URLs like you'd see on any reasonably complex website - merely referring to an address of a page. So I can't break up the URL in the route - all I want is the full string I can then process in code. My regex routes all traffic to the index controller, and $_SERVER['REQUEST_URI'] contains the requested URI - in your example "/Title/edit".



44.7k

It seems to me though that if you eliminate the possibility of "pages" having certain control characters like / in them that you can gain the advantage of the formatlity of the router. This is why I recommended starting with basic latin characters like /([a-zA-Z0-9]+)/:params. If you have remove trailing slashes on then its basically the same. Removing params and the slash is /([a-zA-Z0-9]+). That is basically the same as yours but doesn't match empty string. Then go through there and add huge ranges and swaths of unicode to it.

Then having the + in there means of course one or more. So that won't match it to the default route but probably that should be something different anyways.

If you do it like that then you won't have to use logic in your controllers to break apart the routes. You could use the router for that! Then you could use /([a-zA-Z0-9]+)/edit to be able to create and edit new pages. Also if you reserved some additional characters then you could use that at the beginning of the route like !edit/([a-zA-Z0-9]+).

I just think that you give up a whole lot (everything) by using *. I'm doing something kind of similar at this exact moment. Some of the technological proceeds will go into Webird.

In my particular case, the url musn't be 2 "words" long. It could be 3, 4 10, 20, and the path is entirely up to the users. There's essentially no restriction to the format of the URLs, so trying to make them fit into even a semi-rigid structure of controller/action. In your originally suggested solution, I would still have to break up the "article" parameter just like I'm doing with the full URL now.

Nonetheless, I do appreciate you taking the time for this discussion.



44.7k

How would you make special pages like signin? Is that just declared later on in the routing?

Sign in is done through Shibboleth - before Phalcon even gets touched.

But there will be special pages - true - I imagine they'll be declared in later rules like you suggest.



44.7k

Again I suggest that you use a special character prefix for either the special pages or the content paths. Otherwise you will end up maintaining a whole other list of reserved path names. Even just a /s/ or whatever just makes life so much easier and then maybe just make a reserve list of say s/, signin and signout.

Mark my words: the prophecy has been spoken.

That's a good idea.