Quantcast

ZF2 : How to set/get params in url

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

ZF2 : How to set/get params in url

booradleys
This post has NOT been accepted by the mailing list yet.
Hi there,
I'm trying to generate a url with param 'toto':

$params['controller'] = $module.'-'.$controller;
$params['action'] = $action;  
$params['toto'] = '99';      
$url = $this->view->url('default',$params,array(),FALSE);

But url generated doesn't contains any param 'toto'.

Next, i'm trying to get param 'toto' from url /<namespace-controller>/<action>/toto/99

Then i get:

"An error occurred processing this page; please try again later.

Invalid exception found"

So how can we set/get params in url ?

David
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 : How to set/get params in url

cmple
I'm having the same issue,
Is there a way to pass a $_GET parameter to the URL like in ZF1?

for example:
http://localhost/controller/action/parameterOne/valueOne/parameterTwo/valueTwo

In controller:

$request = $this->getRequest();
$parameters = $request->get()->toArray();

Thanks!
Roman
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 : How to set/get params in url

EvanDotPro
On Thu, Apr 19, 2012 at 9:50 AM, cmple <[hidden email]> wrote:

> I'm having the same issue,
> Is there a way to pass a $_GET parameter to the URL like in ZF1?
>
> for example:
>
> http://localhost/controller/action/*parameterOne*//valueOne//*parameterTwo*//valueTwo/


From what I've seen in the discussions on IRC, these types of parameters
are being deprecated as they pose issues with SEO and indexing your site
with duplicate content. For exampe:

http://yoursite/controller/action/param1/value1/param2/value2

Yields the same result / content as:

http://yoursite/controller/action/param2/value2/param1/value1

This can have a negative impact on your sites indexed status among search
engines as well as diluting your PageRank if you get inbound links to
multiple access points to the same page/content with parameters out of
order.

That said, we do still have a wildcard route, so afiak this type of thing
can still be implemented, we just no longer recommend it. Instead, the
consensus is that you should create distinct, specific routes with the
segments/parameters needed in various situations.

--
Ean Coury
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 : How to set/get params in url

cmple
EvanDotPro wrote
On Thu, Apr 19, 2012 at 9:50 AM, cmple <[hidden email]> wrote:

> I'm having the same issue,
> Is there a way to pass a $_GET parameter to the URL like in ZF1?
>
> for example:
>
> http://localhost/controller/action/*parameterOne*//valueOne//*parameterTwo*//valueTwo/


From what I've seen in the discussions on IRC, these types of parameters
are being deprecated as they pose issues with SEO and indexing your site
with duplicate content. For exampe:

http://yoursite/controller/action/param1/value1/param2/value2

Yields the same result / content as:

http://yoursite/controller/action/param2/value2/param1/value1

This can have a negative impact on your sites indexed status among search
engines as well as diluting your PageRank if you get inbound links to
multiple access points to the same page/content with parameters out of
order.

That said, we do still have a wildcard route, so afiak this type of thing
can still be implemented, we just no longer recommend it. Instead, the
consensus is that you should create distinct, specific routes with the
segments/parameters needed in various situations.

--
Ean Coury
Thanks Evan, it makes sense.

Can you give me an example of a distinct route?

for example:
http://localhost/user/activate/key/0E6E9AF463

how can I setup and retrieve the key parameter?

here is my default route:

'routes' => array(
                        'default' => array(
                            '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' => 'index',
                                    'action'     => 'index',
                                ),
                            ),
                        ),
),

Thanks!!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 : How to set/get params in url

weierophinney
Administrator
In reply to this post by cmple
-- cmple <[hidden email]> wrote
(on Thursday, 19 April 2012, 09:50 AM -0700):

> I'm having the same issue,
> Is there a way to pass a $_GET parameter to the URL like in ZF1?
>
> for example:
> http://localhost/controller/action/*parameterOne*//valueOne//*parameterTwo*//valueTwo/
>
> In controller:
>
> $request = $this->getRequest();
> $parameters = $request->*get()*->toArray();

The question is: do you want $_GET (aka query string) parameters, or do
you want URL parameters?

If you want parameters from the query string, $request->query() is what
you're looking for. You can either cast it to an array (toArray()), or
grab specific parameters out of it (get($name, $default)).

    $allGetParameters = $request->query()->toArray();
    $oneGetParameter  = $request->query()->get('foo', 'default value');

If you want URL parameters -- which is what you actually illustrate
above -- you'll need to do two things. First, make sure there's a
wildcard segment on your route. As an example, you might have routes
like this setup:

    'routes' => array(
        'default' => array(
            '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' => 'Application\Controller\IndexController',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'wildcard' => array(
                    'type' => 'Wildcard',
                ),
            ),
        ),
    ),

What the above says is that it expects /:controller/:action, and then if
there are additional segments, it passes them on to the child "Wildcard"
route. Now, if anything matches in the wildcard route, you can then grab
it from the RouteMatch in your controller.

    $event   = $this->getEvent();
    $matches = $event->getRouteMatch();

    $value = $matches->get('someParameter', 'default value');

Hope all of this information helps you out!

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 : How to set/get params in url

cmple
weierophinney wrote
-- cmple <[hidden email]> wrote
(on Thursday, 19 April 2012, 09:50 AM -0700):
> I'm having the same issue,
> Is there a way to pass a $_GET parameter to the URL like in ZF1?
>
> for example:
> http://localhost/controller/action/*parameterOne*//valueOne//*parameterTwo*//valueTwo/
>
> In controller:
>
> $request = $this->getRequest();
> $parameters = $request->*get()*->toArray();

The question is: do you want $_GET (aka query string) parameters, or do
you want URL parameters?

If you want parameters from the query string, $request->query() is what
you're looking for. You can either cast it to an array (toArray()), or
grab specific parameters out of it (get($name, $default)).

    $allGetParameters = $request->query()->toArray();
    $oneGetParameter  = $request->query()->get('foo', 'default value');

If you want URL parameters -- which is what you actually illustrate
above -- you'll need to do two things. First, make sure there's a
wildcard segment on your route. As an example, you might have routes
like this setup:

    'routes' => array(
        'default' => array(
            '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' => 'Application\Controller\IndexController',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'wildcard' => array(
                    'type' => 'Wildcard',
                ),
            ),
        ),
    ),

What the above says is that it expects /:controller/:action, and then if
there are additional segments, it passes them on to the child "Wildcard"
route. Now, if anything matches in the wildcard route, you can then grab
it from the RouteMatch in your controller.

    $event   = $this->getEvent();
    $matches = $event->getRouteMatch();

    $value = $matches->get('someParameter', 'default value');

Hope all of this information helps you out!

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]
Thanks for the help Matthew!
I also agree with Evan on creating specific routes instead of using wildcards,
So after some digging around I got this working:

'Zend\Mvc\Router\RouteStack' => array(
    'parameters' => array(
        'routes' => array(
       
            'Application\Controller\UserController' => array(
                'type' => 'Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'user',
                        'action' => 'index',
                    ),
                ),
               
                'child_routes' => array(
                    'activate' => array(
                    'type' => 'Segment',
                    'options' => array(
                                        'route'    => 'activate[/:key]',
                                        'defaults' => array(
                                            'controller' => 'user',
                                            'action'     => 'activate',
                                        ),
                                    ),
                    ),
                    ),
                 ),
        ),
    ),
),


Controller:
...
public function activateAction(){
    $key = $this->getEvent()->getRouteMatch()->getParam('key');
    //$key == 104BF9B351
}

URL:
http://localhost/activate/104BF9B351
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2 : How to set/get params in url

intellix
Cool. I was under the impression this was a bug because in the Akrabat Zend Skeleton it used to have

<a href="<?php echo $this->url(
    'controller' => 'album',
    'action' => 'delete'
) ?>?id=1">Delete

So I can see after looking at his latest tutorial that he now passes it like:

<a href="<?php echo $this->url(
    'controller' => 'album',
    'action' => 'delete',
    'id' => 1
) ?>">Delete

So I can go into my app and start changing it all back :) I do agree with the SEO duplicate pages as we've had that problem with ZF1 at work and having to implement canonical tags everywhere etc.

I likes what I see
Loading...