Comments (7)
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.
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.
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.
@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.
I had. It left me more confused than when I started.
from zf2-restful-module-skeleton.
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.
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)
- How to write unit test for a controller HOT 4
- Missing instance/object for parameter route for Zend\Mvc\Router... HOT 1
- Extra data when issueing Status Code HOT 1
- This module does not work with ZendDeveloperTools HOT 3
- not composer friendly
- Do we have Unit tests for this module?
- multiple get method HOT 2
- getVariables() method issue HOT 1
- Service with name "json-pp" could not be created. HOT 1
- The requested URL could not be matched by routing HOT 4
- Fatal error HOT 2
- Question about posible reason not function HOT 2
- unable to fetch or create an instance for info HOT 3
- Error Processor takes over all application, not just Rest Module HOT 14
- Response format in URI HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zf2-restful-module-skeleton.