Jay Zus
Jay Zus

Reputation: 573

Most efficient way to rewrite URL

I'm working on a project with Zend Framework

Users will be able to post announcement on the website.

For now you can consult them with an URL formatted like (for the announcement with id = 60)

mysite.com/announcement/view/id/60

The result I want, for a better SEO, would be under the form of

mysite.com/announcement/[CITY NAME]/[ZIPCODE]/[TYPE OF GOOD]/[STREET NAME]-[ID]

all of [VARIABLE] are stored in a database and easily accessible.

However, I came up with solutions poorly efficient so I'd like to know the different ways to make the URL rewriting, and the most efficient ones.

For now, I thought about generating the URL the "hard" way when the users post their announcement in a database, and create new routes in my Bootstrap.php when the user connect. This seems ok but I think that it could take a really long time when the volume of announcement will be big.

The second solution I thought of was to generate directly the URL in a file, like an .xml file, that I could load when the user connects too, but that will pose the same problem.

Is there a way to create theses routes efficiently ?

The solution of the RewriteEngine of Apache doesn't seem to be quite flexible for that kind of wanted behaviour ...

For information, the volume of announcement will most likely be over 10 000, so the problem of efficiency is quite big.

Upvotes: 2

Views: 117

Answers (2)

Robert
Robert

Reputation: 20286

The best solution is to use Zend_Controller_Router_Route_Regex

Example

$route = new Zend_Controller_Router_Route_Regex(
 'announcement/(.*?)/(.*?)/(.*?)/(.*?)\-([0-9]*)',
  array(
    'announcement' => 'archive',
    'action' => 'view'
  ),
  array(
    1 => 'city_name',
    2 => 'zipcode',
    3 => 'type_of_good',
    4 => 'street_name',
    5 => 'id'
    )
);
$router->addRoute('archive', $route);

It will assign subpatterns to variables where 1=> is first subpattern, 2 is second subpattern etc. These variables will be available in action method with for example $this->_getParam('zipcode'); You can adjust REGEX to your needs.

Upvotes: 2

Orangepill
Orangepill

Reputation: 24655

With zend framework you don't have to assign every endpoint as it's own just just the layout

$route = new Zend_Controller_Router_Route(
            'announcement/:city_name/:zipcode/:type_of_good/:street_name/:id',
            array("controller"=>"accouncement", "action"=>"view-details"));

within the controller then you can just use them as

$this->_request->getParam("zipcode");

Upvotes: 2

Related Questions