Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for longer (or more complex) URLs #15

Open
daangemist opened this issue Feb 22, 2013 · 7 comments
Open

Support for longer (or more complex) URLs #15

daangemist opened this issue Feb 22, 2013 · 7 comments
Labels

Comments

@daangemist
Copy link

Hi,

I am looking into supporting more fine-grained URLs, for example like the Twitter API:

statuses/mentions_timeline
statuses/user_timeline

Is this something you can help me with or point me in the right direction?

Regards,

Daan

@vasrap
Copy link
Owner

vasrap commented Feb 22, 2013

You can use routes. For example create a specific route per URL in .../module/Main/configs/module.config.php:

'router' => array(
    'routes' => array(
        '0' => array(
            'type'    => 'literal',
            'options' => array(
                'route'    => '/statuses'
            ),
            'may_terminate' => false,
            'child_routes'  => array(
                'mentions-timeline' => array(
                    'type'    => 'literal',
                    'options' => array(
                        'route'    => '/mentions_timeline',
                        'defaults' => array(
                            'controller' => 'statuses',
                        ),
                    ),
                    'may_terminate' => true,
                ),
                'user-timeline' => array(
                    'type'    => 'literal',
                    'options' => array(
                        'route'    => '/user_timeline',
                        'defaults' => array(
                            'controller' => 'statuses',
                        ),
                    ),
                    'may_terminate' => true,
                ),
            )
        )
    )
)

Create the StatusesController where the above URLs are now mapped.

In the StatusesController and within the target HTTP method sniff the route name and act appropriately:

$routeMatch = $this->getEvent()->getRouteMatch();
if ($routeMatch->getMatchedRouteName() === 'mentions-timeline') {
    // Handle "statuses/mentions_timeline"
} else if ($routeMatch->getMatchedRouteName() === 'user-timeline') {
    // Handle "statuses/user_timeline"
}

I suggest using the controller just to sniff the route and then redirect code flow to some sort of library code to keep your controllers light.

Let me know if you have more questions.

@daangemist
Copy link
Author

Thanks for the quick response. I somehow focused to much on the predefined routes in the module, instead of thinking about custom routing. :)

@vasrap vasrap reopened this Feb 22, 2013
@daveo1001
Copy link

What's a guy to do if he wants,
/account/ -> list accounts
/account/accountID/ -> account info
/account/accountID/user -> list users
/account/accountID/user/userID -> user info

I used your stock module.config.php and got the account and account/accountID stuff going but i'm not sure how to set up the child_routes (do i even want to use that?). I'm not sure how (or if they can) they co-exist with options stuff in the main route. Like, how does the application know it's an account id or a route to travel. Yeesh, this stuff is over my head.

@vasrap
Copy link
Owner

vasrap commented Aug 1, 2013

@daveo1001 RTFM? :) -- http://framework.zend.com/manual/2.0/en/modules/zend.mvc.routing.html

-- "Like, how does the application know it's an account id or a route to travel."
It knows because the resource name and id will need to be detected by a different regex.

@daveo1001
Copy link

I had. It left me more confused than when I started.

@trainerbill
Copy link

I am having a similar issue as @daveo1001. I am try to do a nested call /object/id/nestedobject[/:id] but the request is always going to the get action on the nested controller. I am guessing because the first id is set. So how would you go about doing a nested rest route?

'nvpapicalls-rest' => array(
                            'type'=>'Segment',
                            'options' => array(
                                'route' => '/nvpapicalls[.:formatter][/:id]',
                                'constraints' => array(
                                    'formatter'  => '[a-zA-Z][a-zA-Z0-9_-]*',
                                    'id'     => '[0-9]+',
                                ),
                                'defaults' => array(
                                        'controller' => 'nvpapicalls-rest',

                                ),
                            ),
                            'may_terminate' => true,
                            'child_routes' => array(

                                'comments' => array(
                                    'type'=>'Segment',
                                    'options' => array(
                                        'route' => '/comments[/:id]',
                                        'constraints' => array(
                                            'action'  => '[a-zA-Z][a-zA-Z0-9_-]*',
                                            'id'     => '[0-9]+',
                                        ),
                                        'defaults' => array(
                                            'controller' => 'nvpcomments-rest',
                                            //'action' => 'list'

                                        ),
                                    ),
                                ),
                            ),

                        ),

When i go to /nvpapicalls.json/10/comments it is being dispatched to the nvpcomments-rest controller but to the get action and not the getList action. Any idea?

Update: Found a work around. I just changed the :id to :commentid and used $this->setIdentifierName('commentid'); in the construct of the controller. Anyone know if there is a routing configuration option for this?

@isacharsilva
Copy link

hi how can i access the create method using your restful APi thanks real need it i am new to restful using zf2 thanks iam using jquery mobile by the way in ajax request please help me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants