How to pass parameters by url with Zend Framework 2 - php

I'm getting into trouble with passing param by url in Zend Framework 2. For example : I want to access page by a URL like this:
http://example.com/student/details/aaa it's working, but if my url like this :
http://example.com/student/details/1 it's not working, And i still get a 404.
I tried to follow the instruction from how to pass params using a route in Zend Framework 2?
But it still doesn't work.
here's my module.config.php
'student' => array(
'type' => 'segment',
'options' => array(
'route' => '/student[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'__NAMESPACE__' => 'AppFeeder\Controller',
'controller' => 'Student',
'action' => 'index',
),
),
),
And i use this $this->getEvent()->getRouteMatch()->getParam('id') in my controller.
Can anyone help me ?

You are using wrong route rule for id which says start from alphabet then alphabet+number+"_-".
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
Instead you need to change the rule that says start from alphabet+number then alphabet+number+"_-"
'id' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',

Related

Zend framework routing error

I have a problem with a routing in Zend framework.
'name' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/site/:id/orders[/:page]',
'constraints' => array(
'id' => '[0-9]*',
'page' => '[0-9]*'
),
'defaults' => array(
'controller' => 'Application\Controller\Site',
'action' => 'action'
),
),
),
And in a controller.
$id = (int) $this->params()->fromRoute('id');
And in some (!) cases a browser returns this error - "Missing parameter 'id'", but I don't know why.
Can anybody help me on this issue?
well based on your route configuration id must exist in your routes, so the link you requested didn't have id . and your constrains also should change to 'id' => '[0-9]+' so the id must exist.
and also you get the id in the controller by just typing
$id=$this->params("id");
which will get the id too

how to create an optional route parameter for zend framework 2 restful route

I'm working with zend framework 2 and I need to create an optional parameter for a route segment that also has two required parameters. Below is a snippet from the module.config.php describing the route. My understanding is that in ZF2 an optional route parameter can be created by using the
[/:param]
which you can see is what I have. It works fine as long as I send the optional param, but when I leave it out the first two params "uname and appname" are appended together into the "uname" constraint. The route is a parent route.
'roles' => array(
'type' => 'segment',
'options' => array(
'route' => '/roles/:uname/:appname[/:locnames]',
'constraints' => array(
'uname' => '[a-zA-Z].+',
'appname' => '[a-zA-Z0-9_-].+',
'locnames' => 'locnames'
),
'defaults' => array(
'controller' => 'Roles/Controller/RolesController'
),
),
),
What am I missing here, I know you can have define optional parameters, but I can't figure out the correct format
Thanks to grizzm0 on #zftalk or helping me with this one. It was a simple regular expressions issue. Removing the dot(.) in the constraints correctly matched the incoming url parameters. So my route now looks like this:
'roles' => array(
'type' => 'segment',
'options' => array(
'route' => '/roles[/:action][/uname/:uname][/appname/:appname][/locnames/:locnames]',
'constraints' => array(
'uname' => '[a-zA-Z]+',
'appname' => '[a-zA-Z0-9_-]+',
'locnames' => 'locnames'
),
'defaults' => array(
'controller' => 'Roles/Controller/RolesController'
),
),
),
'roles' => array(
'type' => 'segment',
'options' => array(
'route' => '/roles[/:action][/uname/:uname][/appname/:appname][/locnames/:locnames]',
'constraints' => array(
'uname' => '[a-zA-Z].+',
'appname' => '[a-zA-Z0-9_-].+',
'locnames' => 'locnames'
),
'defaults' => array(
'controller' => 'Roles/Controller/RolesController'
),
),
),
You can configure your route like this way.
Here inside your roles controller
you have some action live index.
so your route will be
siteurl/roles/index/uname/john/appname/stackexchange/locanames/yourlocanames
here if you don't want to write appname then remove youre paramater so your route will work.

Render header with /abc/abc/abc php

I have experience with php but i'm new to the zend frameworks. Could some one please give me some leads as to how I can get a site to work ie- www.facebook.com/nathandoe/photos
.
Using controllers how can i create a function that accepts different names and direct it that page ?
if i have a site www.hello.com/profile
instead of using GET www.hello.com/profile?is=342342?photos
how to handle it as www.hello.com/profile/342342/photos
i have controllers that handle the index and i know how to create different functions .. im stuck with www.hello.com/profile/xxxxxxxx/photos that part
In your module/[Module Name]/config/module.config.php, you can set the routes.
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
Something like
'route' => '/profile/[/:id]/photos',
Reference http://framework.zend.com/manual/2.0/en/user-guide/routing-and-controllers.html

Using route in Zend Framework 2

i need to route urls like in zf1.
in particular i need that these urls will be automatically redirect to appropriate actions without specify a new route every time.
/site/getData
/site/getData?param=5&par2=test
/site/getOther
...
So a segment route doesn't work, i've tried a Literal route but i can't reach a working solutions.
Anyone can help me?
Thanks a lot
This should be solved by a pretty default segment route like the one provided in the documentation.
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/:controller[/:action]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]+',
'action' => '[a-zA-Z][a-zA-Z0-9_-]+',
),
'defaults' => array(
'controller' => 'default-controller-alias',
'action' => 'index',
),
)
Now if you set up your controller names like the following:
'controllers' => array(
'invokables' => array(
'sites' => 'Namespace\Controller\SitesController',
'other' => 'Namespace\Controller\OtherController'
Then you should be able to achieve exactly what you want. And to create params to your route, you simply use the ViewHelper correctly ;)
$this->url('routename',
array(
'controller' => 'site',
'action' => 'getData'
),
array (
'query' => array(
'param1' => 'foo',
'param2' => 'bar',
'paramN' => 'baz',
)
)
)

Zend Framework 2 Part Route Assembly

My Zend Framework 2 application has a route definition that's trying to mimic the default Zend Framework 1 route. It looks like:
'router' => array(
'routes' => array(
'default' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'wildcard' => array(
'type' => 'wildcard',
),
),
),
),
),
It matches routes just fine, but I can't assemble routes with arbitrary parameters using the Url view helper.
For example,
$this->url('default', array('controller' => 'test', 'action' => 'test', 'id' => 5));
results in /test/test instead of /test/test/id/5.
Does anyone know how to assemble partial routes like this? Or is there a better way of getting ZF1-style routes?
It turns out that you need to specify the entire route name (including child routes) in the Url view helper.
Using the router defined in my question, the proper view helper call would look like:
$this->url('default/wildcard', array('controller' => 'test', 'action' => 'test', 'id' => 5));
which would result in a url of /test/test/id/5.

Categories