Ok, now I understand.
It's hard to say. It depends on your needs.

The easiest way to get a route param inside a controller is by calling
$this->params('env', 'default'). That will return the value of the param
from the route with the name env, or the default value if it wasn't defined.
You can do that on each action that needs to return different information
depending on the environment.

If you need to access the environment inside a Service, you could inject
the RouteMatch object inside that Service, which will allow you to get the
route params from it by calling $routeMatch->getParam('env', 'default'), in
a similar way as you did in the last case.
This could be the example of one of your service factories where you inject
the RouteMatch on your service

...
public function createService(ServiceLocatorInterface $sm)
{
    $app = $sm->get('Application');
    $routeMatch = $app->getMvcEvent()->getRouteMatch();
    return new MyService($routeMatch);
}
...


You could even inject only that route param instead of the whole RouteMatch
object, if you prefer.

If you are going to send that value on each request I think saving it in
the database at bootstrap to be able to get it later is not a good idea.
That will unnecessarily slow down your application.

Saving it anywhere globally also at bootstrap is not a good idea either.
The "global" concept is not a good practice.

I don't know if I answered your question. Let's see if anyone else has a
better approach for this.

Best regards!



2014-08-01 11:40 GMT+02:00 Emmanuel Bouton <[email protected]>:

> Sorry I missed your last message ...
>
> My question is : now that I have a proper way to forward my environment id
> in each requests once it has been selected, I'd like to know where I can
> globally manage this parameter :
>  - Get the id from url
>  - Retrieve the environment in database
>  - Store the « selected environment » somewhere where all my controllers
> can say : « ok there is a selected environment, so i have to filter all my
> results with this environment ».
>
> I know how I can do these treatments, but not yet where I have to implement
> them (in the boostrap of the module ? Attaching it to a MVC event ?), and
> where to store the selected environment (in the service manager ?).
>
> Thanks,
> Emmanuel
>
> 2014-07-26 7:15 GMT+02:00 Alejandro Celaya <[email protected]>:
>
> > I'm not sure if I understood you. You mean how to deal with database
> > persistence in Zend framework 2?
> >
> > --
> > Alejandro Celaya Alastrué
> > http://www.alejandrocelaya.com
> > El 26/07/2014 01:28, "Emmanuel Bouton" <[email protected]> escribió:
> >
> > Wow it seems exactly what I need thanks a lot :)
> >>
> >> Another thing, how would you manage globally the treatment of the
> selected
> >> environment (retrieving the environment in the database) ?
> >> And how should I store the environment so that I could use it when
> needed
> >> ?
> >>
> >>
> >> 2014-07-25 22:18 GMT+02:00 Alejandro Celaya <[email protected]
> >:
> >>
> >> > Hi!
> >> >
> >> > You could define an optional parameter in your route and then use the
> >> Url
> >> > view helper to inherit that value.
> >> > When the param is not set in your current route it will use the
> default
> >> > value (or no value), otherwise it will use the current value.
> >> >
> >> > For example, If you have defined this routes:
> >> >
> >> > 'home' => [
> >> >     'type' => 'Zend\Mvc\Router\Http\Segment',
> >> >     'options' => [
> >> >         'route'    => '/[:env]',
> >> >         'defaults' => [
> >> >             'controller' => 'Application\Controller\Index',
> >> >             'action'     => 'index'
> >> >         ],
> >> >     ],
> >> > ],
> >> >
> >> > 'another-route' => array(
> >> >     'type' => 'Zend\Mvc\Router\Http\Segment',
> >> >     'options' => array(
> >> >         'route'    => '/foo/bar/[:env]',
> >> >         'defaults' => array(
> >> >             'controller' => 'Application\Controller\Foo',
> >> >             'action'     => 'bar'
> >> >         ),
> >> >     ),
> >> > ),
> >> >
> >> >
> >> > When you are in www.mydomain.com/, if you use the URL view helper
> like
> >> > this, $this->url('another-route', [], true), it will produce the route
> >> > '/foo/bar/'
> >> > Otherwise, if you are in www.mydomain.com/another-env, the same
> >> previous
> >> > usage of the URL view helper will produce the route
> >> '/foo/bar/another-env'
> >> > This is because of the last boolean parameter of the URL view helper,
> >> > which defaults to false. In any place where you don't want the env
> >> > parameter to be inherited, just don't use the inheritance and set it
> >> > yourself like this $this->url('another-royute', ['env' =>
> >> 'another-one'])
> >> >
> >> > You could define constraints for your env if you need it, and also
> >> define
> >> > a default value.
> >> >
> >> > Best regards.
> >> >
> >> >
> >> >
> >> > 2014-07-25 21:51 GMT+02:00 Emmanuel Bouton <[email protected]>:
> >> >
> >> > Hello,
> >> >>
> >> >> I'm working on a web admin that manages a list of servers.
> >> >> In the header of all the pages there's a select box for the servers
> >> >> environments.
> >> >>
> >> >> When no environment is selected, all the pages concerns all the
> >> servers.
> >> >> When an environment is selected, all the pages concerns only the
> >> server of
> >> >> this specific environment.
> >> >>
> >> >> The easy way to manage the current environment in all my controllers
> >> would
> >> >> be to store it in the session. But I'd rather send it on each
> request.
> >> >> That
> >> >> would allow to work on multiple environments in several tabs of the
> >> same
> >> >> browser.
> >> >>
> >> >> How would you do that ?
> >> >> Is there a proper way to change all urls in views to add the
> >> environment
> >> >> id
> >> >> when it has been selected ? maybe in the routes ?
> >> >>
> >> >> Thanks,
> >> >> Emmanuel
> >> >>
> >> >
> >> >
> >> >
> >> > --
> >> > Alejandro Celaya Alastrué
> >> > http://www.alejandrocelaya.com
> >> >
> >>
> >
>



-- 
Alejandro Celaya Alastrué
http://www.alejandrocelaya.com

Reply via email to