Quantcast

Friendly URLs

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Friendly URLs

Aycko Maerzke
Hi,

I'm currently working on browser friendly URLs which represents a virtual
directory tree like:

http://yourdomain.com/menu/submenu/subsubmenu/mypost/ 

"menu", "submenu" and "subsubmenu" represents the navigation tree, "mypost"
ist he alias of an article that i will display.

Now there is a problem: I can create all available routes für this
navigation tree, but this could be easily over 600 routes.
For each route exists an object and all routes must be matched from the
RewriterRouter.
I think this is not the best solution for the performance.
Anybody have some experience to solve this problem?


Best regards Aycko
(Sorry for my bad english^^)



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Friendly URLs

Marcel de Graaf
Hello Aycko,

For the website I'm currently making, I used a similar structure to be able to
use pretty URL's instead of the ugly "?id=2234&page=746&prod=234" kind of
way.
Please note that I didn't use the ZF for this, I hadn't heard of it's
existence until about a week ago. I can only give you an idea of how I solved
the problem.

I made a .htaccess-file with a RewriteRule that passes all requests to the
index.php-file. In this file, the first thing I do is to read the
$_SERVER['REQUEST_URI'] variable, which (in my case) is of the
form "products/<product_id>", or "cart/add/<product_id>".
I simply explode these values on the "/" (with some built-in safety of
course), and depending on what's requested, I include certain pages and pass
variables to these pages where needed.

Again, I have no idea (yet) how the ZF solves this, and I don't know if you
can do anything with my solution, but I thouht I could at least share my
solution with you, it can' t do any harm ;-).

Kind regards,


Marcel de Graaf.


On Wednesday August 30, 2006 11:58, Aycko Maerzke wrote:

> Hi,
>
> I'm currently working on browser friendly URLs which represents a virtual
> directory tree like:
>
> http://yourdomain.com/menu/submenu/subsubmenu/mypost/
>
> "menu", "submenu" and "subsubmenu" represents the navigation tree, "mypost"
> ist he alias of an article that i will display.
>
> Now there is a problem: I can create all available routes für this
> navigation tree, but this could be easily over 600 routes.
> For each route exists an object and all routes must be matched from the
> RewriterRouter.
> I think this is not the best solution for the performance.
> Anybody have some experience to solve this problem?
>
>
> Best regards Aycko
> (Sorry for my bad english^^)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Friendly URLs

Martel
In reply to this post by Aycko Maerzke
Aycko Maerzke wrote:

> I'm currently working on browser friendly URLs which represents a virtual
> directory tree like:
> http://yourdomain.com/menu/submenu/subsubmenu/mypost/ 
>
> "menu", "submenu" and "subsubmenu" represents the navigation tree, "mypost"
> ist he alias of an article that i will display.
>
> Now there is a problem: I can create all available routes für this
> navigation tree, but this could be easily over 600 routes.
 > For each route exists an object and all routes must be matched from the
 > RewriterRouter.

One route should be sufficient for this task.

Zend_Controller_Router_Route(
  ':menu/:submenu/:subsubmenu/:item',
  array('controller' => 'ctrl', 'action' => 'act',
        'submenu' => null, 'subsubmenu' => null, 'item' => null)
);

You may add a wildcard to the end of the route definition to get the rest of
the parameters in the shape of "/var/value" pairs:

':menu/:submenu/:subsubmenu/:item/*'

Both will match URIs like:

http://yourdomain.com/menu
http://yourdomain.com/menu/submenu
http://yourdomain.com/menu/submenu/subsubmenu
http://yourdomain.com/menu/submenu/subsubmenu/mypost/

Second will match:
http://yourdomain.com/menu/submenu/subsubmenu/mypost/var1/value1/var2/value2

--
Michael Minicki aka Martel Valgoerad | [hidden email] | http://aie.pl/martel.asc
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
"Finance is the art of passing money from hand to hand until it finally
disappears." -- Robert W. Sarnoff
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Friendly URLs

Zielun
Sorry for refreshing so old thread but I have question to the example.
What if I don`t have the max depth of menu/submenu?
I mean in the above example there can be two submenu: submenu and subsubmenu. What if there could be more? Is there a way to define it less static?

THanks for any clue.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Friendly URLs

Matthew Ratzloff

-Matt

On Mon, Dec 1, 2008 at 2:47 AM, Michał Zieliński <[hidden email]> wrote:

Sorry for refreshing so old thread but I have question to the example.
What if I don`t have the max depth of menu/submenu?
I mean in the above example there can be two submenu: submenu and
subsubmenu. What if there could be more? Is there a way to define it less
static?

THanks for any clue.
--
View this message in context: http://www.nabble.com/Friendly-URLs-tp6055801p20767571.html
Sent from the Zend Framework mailing list archive at Nabble.com.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Friendly URLs

Zielun
Thanks Matt.

I create route like this:
routes.x.type = "Zend_Controller_Router_Route_Regex"
routes.x.route = "(.*)"
routes.x.defaults.module = "default"
routes.x.defaults.controller = "section"
routes.x.defaults.action = "load"
routes.x.map.1 = "path"

For uri like /about/history/1999/
I get output like:
array(4) {
  ["path"] => string(2) "about/history/1999"
  ["module"] => string(7) "default"
  ["controller"] => string(5) "section"
  ["action"] => string(5) "load"
}

So it is fine, I can easily load appropriate site section (section: about, subsection: history, subsubsection: 1999).

But if I have a defined controller like subscription, or different module like admin I can`t use them. I could redirect to it from within SectionController::loadAction where every request goes but it isn`t the best solution I believe.

I was thinking about plugin which could decide if additional routing is needed or maybe custom route but (in both options) at the moment when routes are loaded I guess front controller methods like getModuleName or getControllerName don`t exist or return null. So I`m not sure how could I make this decision (if controller exists load it, else use route regex like "(.*)".

How it should be done?

@edit
looking at the errorHandler plugin I see that this approach could also be used:


switch ($exceptionType) {
                case 'Zend_Controller_Dispatcher_Exception':
                    $error->type = self::EXCEPTION_NO_CONTROLLER;
                    break;


So I could write similar plugin for this or basically use ErrorController::errorAction to handle the logic for retriving section from db. However, I don`t know if this plugin (errorAction method) was designed to play such role.
I could also try to catch dispatch exception in my app index.php file like this:


$fc = Zend_Controller_Front::getInstance();
try {
  $fc->dispatch();
} catch  (Zend_Controller_Dispatcher_Exception $e) {
  // create router with additional route "(.*)"
  $fc->setRouter($router);
  $fc->dispatch();
} catch (Exception $e) {
  // ...
}


However, I don`t know if this approach is recommended because according to quickstart tutorial I guess front controller configuration (and other config as well) should be in bootstrap file. Index.php should only launch dispatch.
Again, what`s your opinion?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Friendly URLs

Matthew Ratzloff
Why are you sending so many e-mails?  You sent three the first time, and six following my reply.  If this is a bug in your e-mail client, please use a different one.  If this is intentional, please stop.

Now that I think about it some more, you can do one of two things.  You can define a series of routes up to a reasonable maximum number of submenus (hacky, but it's fast and will work) or you can write your own Route class, Your_Controller_Router_Route_Http.  You definitely do not want to match on (.*) and parse it yourself, since you'd basically be writing your own Route class in a non-standard way.

-Matt

On Wed, Dec 3, 2008 at 12:24 AM, Michał Zieliński <[hidden email]> wrote:

Thanks Matt.

I create route like this:
routes.x.type = "Zend_Controller_Router_Route_Regex"
routes.x.route = "(.*)"
routes.x.defaults.module = "default"
routes.x.defaults.controller = "section"
routes.x.defaults.action = "load"
routes.x.map.1 = "path"

For uri like /about/history/1999/
I get output like:
array(4) {
 ["path"] => string(2) "about/history/1999"
 ["module"] => string(7) "default"
 ["controller"] => string(5) "section"
 ["action"] => string(5) "load"
}

So it is fine, I can easily load appropriate site section (section: about,
subsection: history, subsubsection: 1999).

But if I have a defined controller like subscription, or different module
like admin I can`t use them. I could redirect to it from within
SectionController::loadAction where every request goes but it isn`t the best
solution I believe.

I was thinking about plugin which could decide if additional routing is
needed or maybe custom route but (in both options) at the moment when routes
are loaded I guess front controller methods like getModuleName or
getControllerName don`t exist or return null. So I`m not sure how could I
make this decision (if controller exists load it, else use route regex like
"(.*)".

How it should be done?

@edit
looking at the errorHandler plugin I see that this approach could also be
used:


switch ($exceptionType) {
               case 'Zend_Controller_Dispatcher_Exception':
                   $error->type = self::EXCEPTION_NO_CONTROLLER;
                   break;


So I could write similar plugin for this or basically use
ErrorController::errorAction to handle the logic for retriving section from
db. However, I don`t know if this plugin (errorAction method) was designed
to play such role.
I could also try to catch dispatch exception in my app index.php file like
this:


$fc = Zend_Controller_Front::getInstance();
try {
 $fc->dispatch();
} catch  (Zend_Controller_Dispatcher_Exception $e) {
 // create router with additional route "(.*)"
 $fc->setRouter($router);
 $fc->dispatch();
} catch (Exception $e) {
 // ...
}


However, I don`t know if this approach is recommended because according to
quickstart tutorial I guess front controller configuration (and other config
as well) should be in bootstrap file. Index.php should only launch dispatch.
Again, what`s your opinion?
--
View this message in context: http://www.nabble.com/Friendly-URLs-tp6055801p20808404.html
Sent from the Zend Framework mailing list archive at Nabble.com.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Friendly URLs

Zielun
Sorry for the emails. My mistake, won`t happen again :[

Thanks for reply.
Loading...