how to get dynamic URL like mydomain.com/username using zend framework

前端 未结 6 1619
孤街浪徒
孤街浪徒 2020-12-15 15:02

I am developing an application using zend framework. In the application I have to provide a URL for each user like mydomain.com/[username] then public will be able to view

6条回答
  •  天命终不由人
    2020-12-15 15:31

    Create a custom Route (not Router; there is no need to create a custom router!).

    Your new route should extend Zend_Controller_Router_Route_Abstract, and override the match method, which takes Zend_Controller_Request_Abstract $request as the first parameter.

    In your new match method, simply query your database to see if the custom page, user, etc. exists by comparing against $request->getRequestUri(), and if it does, return the relevant routing information. Example:

    return array(
        'module'     => 'default',
        'controller' => 'users',
        'action'     => 'view',
        'user'       => $user
    );
    

    On failure (can't find the correct user, or page, etc.), then:

    return false
    

    This will scale much better than creating a route for every single user, and doesn't require you to mess around with routing too much. It will still work with the standard router, and you can add it to your routes via application.ini as expected.

提交回复
热议问题