2009年10月5日星期一

Re: [fw-mvc] Problem with a simple route

I ran into the same problem when I added member profiles to my website. I ended up with a route like /members/:username.

There is an alternative, but it also come with its own problems (which might be minor but still something to think about).

You could subclass Zend_Controller_Router_Route_Abstract to create your own route that performs a db lookup of the username, and if it doesn't exist, return false in the match() method. This would allow you to use a route like /:username for profiles.

There are a few problems with this approach though... 

1) You need a db lookup on each and every request, unless you put in blacklist. 

2) You'll need to make sure that certain usernames don't break your site. If you have a login page at /login, and some user creates an account with a username of "login", they would either break your login page or not have a profile.

--
Hector


On Sun, Oct 4, 2009 at 11:02 PM, Mert Oztekin <moztekin@anadolusigorta.com.tr> wrote:
Hi Richard,
 
www.mydomain.com/someusername for your user profiles is not a good idea. Because you cannot use your other Controllers. (You cannot be sure that there wont be a same username with your controller names)
 
You should do somethink like
www.mydomain.com/profile/someusername
    protected function _initRoutes()
    {
        $this->router = $this->frontController->getRouter();
        $route = new Zend_Controller_Router_Route(
            'profile/:username/*',
            array(
                'controller'   => 'view',
                'action'       => 'profile'
            )
        );
        $this->router->addRoute('profile', $route);
    }
 
 
 
From: Richard Knop [mailto:risoknop@gmail.com]
Sent: Saturday, October 03, 2009 4:31 PM
To: fw-mvc@lists.zend.com
Subject: [fw-mvc] Problem with a simple route
 
Hello everyone,

On few following lines I will try to explain my problem as clearly as I can. What I am trying to do is match URLs like this:

www.mydomain.com/someusername

To this:

www.mydomain.com/view/profile/username/someusername

To achieve this I have added this code to my bootstrap file:
    protected function _initRoutes()
    {
        $this->router = $this->frontController->getRouter();
        $route = new Zend_Controller_Router_Route(
            ':username',
            array(
                'module'       => 'default',
                'controller'   => 'view',
                'action'       => 'profile'
            )
        );
        $this->router->addRoute('profile', $route);
    }


Now my problems:

1) When I go to www.mydomain.com/someusername I get this error message:
Message: username is not specified
 
Stack trace
 
#0 C:\wamp\www\hunnyhive\library\Zend\Controller\Router\Rewrite.php(441): Zend_Controller_Router_Route->assemble(Array, true, true)
#1 C:\wamp\www\hunnyhive\library\Zend\View\Helper\Url.php(49): Zend_Controller_Router_Rewrite->assemble(Array, NULL, true, true)
#2 [internal function]: Zend_View_Helper_Url->url(Array, NULL, true)
#3 C:\wamp\www\hunnyhive\library\Zend\View\Abstract.php(342): call_user_func_array(Array, Array)
#4 [internal function]: Zend_View_Abstract->__call('url', Array)
#5 C:\wamp\www\hunnyhive\application\modules\default\views\scripts\view\profile.phtml(17): Zend_View->url(Array, NULL, true)
#6 C:\wamp\www\hunnyhive\library\Zend\View.php(108): include('C:\wamp\www\hun...')
#7 C:\wamp\www\hunnyhive\library\Zend\View\Abstract.php(833): Zend_View->_run('C:\wamp\www\hun...')
#8 C:\wamp\www\hunnyhive\library\Zend\Controller\Action\Helper\ViewRenderer.php(903): Zend_View_Abstract->render('view/profile.ph...')
#9 C:\wamp\www\hunnyhive\library\Zend\Controller\Action\Helper\ViewRenderer.php(924): Zend_Controller_Action_Helper_ViewRenderer->renderScript('view/profile.ph...', NULL)
#10 C:\wamp\www\hunnyhive\library\Zend\Controller\Action\Helper\ViewRenderer.php(963): Zend_Controller_Action_Helper_ViewRenderer->render()
#11 C:\wamp\www\hunnyhive\library\Zend\Controller\Action\HelperBroker.php(277): Zend_Controller_Action_Helper_ViewRenderer->postDispatch()
#12 C:\wamp\www\hunnyhive\library\Zend\Controller\Action.php(523): Zend_Controller_Action_HelperBroker->notifyPostDispatch()
#13 C:\wamp\www\hunnyhive\library\Zend\Controller\Dispatcher\Standard.php(289): Zend_Controller_Action->dispatch('profileAction')
#14 C:\wamp\www\hunnyhive\library\Zend\Controller\Front.php(946): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#15 C:\wamp\www\hunnyhive\application\Bootstrap.php(100): Zend_Controller_Front->dispatch()
#16 C:\wamp\www\hunnyhive\library\Zend\Application.php(335): Bootstrap->run()
#17 C:\wamp\www\hunnyhive\public\index.php(33): Zend_Application->run()
#18 {main}
 
Request parameters:
 
array(4) { ["username"]=>  string(5) "admin" ["module"]=>  string(7) "default" ["controller"]=>  string(4) "view" ["action"]=>  string(7) "profile" }


When I go to www.mydomain.com/view/profile/username/someusername everything works fine though.

2) Second problem is that when I go to URLs such as www.mydomain.com/somecontroller (and somecontroller is and existing controller) I also get a huge error because the URL is matched to the www.mydomain.com/view/profile/username/somecontroller.

How can I solve this problem?
View this message in context: Problem with a simple route
Sent from the Zend MVC mailing list archive at Nabble.com.
 

  ________________________________  
Bu mesaj ve ekleri, mesajda gönderildiği belirtilen kişi/kişilere özeldir ve gizlidir. Size yanlışlıkla ulaşmışsa lütfen gönderen kisiyi bilgilendiriniz ve mesajı sisteminizden siliniz. Mesaj ve eklerinin içeriği ile ilgili olarak şirketimizin herhangi bir hukuki sorumluluğu bulunmamaktadır. Şirketimiz mesajın ve bilgilerinin size değişikliğe uğrayarak veya geç ulaşmasından, bütünlüğünün ve gizliliğinin korunamamasından, virüs içermesinden ve bilgisayar sisteminize verebileceği herhangi bir zarardan sorumlu tutulamaz.

This message and attachments are confidential and intended for the individual(s) stated in this message. If you received this message in error, please immediately notify the sender and delete it from your system. Our company has no legal responsibility for the contents of the message and its attachments. Our company shall have no liability for any changes or late receiving, loss of integrity and confidentiality, viruses and any damages caused in anyway to your computer system.

没有评论: