0
votes

I have the following issue with a website built upon Zend that I can't fix: The website that I am editing (not coded by me) has the following link structure: http://www.site.tld/controller/action

However, I want to get rid of the controller name on links and I can't get it to work. For example, when I click on "Link1" I want to get http://www.site.tld/Link1

I have the following file structure

application
library
public
trial
  .htaccess
  index.php

The .htaccess in the public folder has the following contents:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

If it is helpful, I also observe that each action in the index controller makes use of

$this->_redirect('/index/action');

I tried rewriting the .htaccess file but in vain. It basically didn't point to the physical index controller. Please advise.

Thank you!

2
I tried the solution given by brady.vitrano but it is not working for me, is there another way to hide the controller and action name and also the user should not be able to edit the URL.jjnguy

2 Answers

3
votes

Your looking in the wrong place. The problem is not in your .haccess file but it's how the Zend Framework routes URL's to the proper Controller and Action. What your asking is to override the default route of the Zend_Controller_Router. Like @Adrian has said, "The router is your friend here". I will provide you with an answer to your problem but it comes with a big WARNING:

The following will override your default route and prevent you from visiting controllers and modules that are opened via URLs like: example.com/name.

Place this in your /application/Bootstrap.php file:

public function _initCustomRoute()
{
    $router = Zend_Controller_Front::getInstance()->getRouter();
    $route = new Zend_Controller_Router_Route(':action', array(
        'module'     => 'default',
        'controller' => 'index',
        'action'     => 'index'
    ));
    $router->addRoute('default-override', $route);

}

Note: There are ways of testing if a request can be dispatched and route to this location only on failures, but that's another topic.

2
votes

The router is your friend here, check the documentation on the Zend_Controller_Router and how to set it up in your bootstrap. It would be too much to guide through that setup in a few words.

Update - Addition to your question:
Not sure where you are right now and how 'default' your setup is. Here is a quick rundown on the router configuration in a standard default setup

URL: /home/user
// translates in a default route setup into
home = controller - HomeController.php
user = action     - HomeController::userAction()
// by default you must have a controller named home and within it an action named user
// however, you can override this default to something like this
home = controller - FloridaController.php
user = action     - FloridaController::miamiAction()
// if you add the colon in your config you will change the placeholder in the url to a variable
:user = action     - FloridaController::miamiAction()
URL: /home/John /home/Mike
// all urls are routed to FloridaController::miamiAction()

There are quite a few ways how you can setup the configuration for the routes.