GithubHelp home page GithubHelp logo

Comments (7)

scaraveos avatar scaraveos commented on July 3, 2024

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.

from zf2-restful-module-skeleton.

daangemist avatar daangemist commented on July 3, 2024

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

from zf2-restful-module-skeleton.

daveo1001 avatar daveo1001 commented on July 3, 2024

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.

from zf2-restful-module-skeleton.

scaraveos avatar scaraveos commented on July 3, 2024

@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.

from zf2-restful-module-skeleton.

daveo1001 avatar daveo1001 commented on July 3, 2024

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

from zf2-restful-module-skeleton.

trainerbill avatar trainerbill commented on July 3, 2024

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?

from zf2-restful-module-skeleton.

isacharsilva avatar isacharsilva commented on July 3, 2024

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

from zf2-restful-module-skeleton.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.