zf2 - Retrieve the 'ApplicationConfig' in a 'ActionController' - zend-framework2

I realise that there shouldn't be to many reasons for us to access the ApplicationConfig from a controller. But I'm still asking.. Is there a way to do that?
What I actually wishes to do is to get a hold of the
module_listener_options -> config_glob_paths key...

I think #timdev didn't get your point, your question could be solved by:
$applicationConfig = $this->getServiceLocator()->get('ApplicationConfig');

<?php
// in a controller (or anything that is ServiceLocatorAware)
$config = $this->getServiceLocator()->get('Config');
Will give you an array of your application's merged configuration data.

Related

How to add a request wise variable in httpcontext

In HttpContext(Or some thing like this) I need to add a temperory variable from a controller which need to available through out the request processing(Request wise variable). But the HttpContext.Current.Request is readonly. If i'm adding in Items its not getting outside. How can i achieve this
Thanks & Regards
Binesh Nambiar C
You are looking for HttpContext.Items, which is a dictionary that can be used to store items for the duration of the current request. It goes out of scope at the end of the request.
// Set
HttpContext.Items["Customer"] = customer;
// Get
var customer = HttpContext.Items["Customer"];

Zf2 Redis Adapter, getItems using wildcards

I'm making my first steps in using Redis under ZF2.
I was wondering if there is a method to retrieve keys by pattern.
e.g.:
after setting multiple values with keys like: 'stackOverflow_'.time(), i would like to retrieve later all keys matching the 'stackOverflow_' pattern.
tried using getItems(array $keys) with wildcard in: \vendor\zendframework\zendframework\library\Zend\Cache\Storage\Adapter\AbstractAdapter.php
$redisKeyPattern = 'stackOverflow_';
$redis = $this->getServiceLocator()->get('Redis');
$values = $redis->getItems(array($redisKeyPattern.'*'));
with no succces.
any ideas?
UDPATE:
thanks guys. i ended up with duplicating the Redis adapter and adding my own functionality that utilizes the 'keys' function in the Redis extension:
public function getItemsByKeyPattern($pattern) {
$keys = $this->getRedisResource()->keys('*'.$pattern.'*');
if(empty($keys)) return null;
foreach($keys as &$key){
$key = explode(':', $key)[1];
}
$items = parent::getItems($keys);
return $items;
}
and it works for me :)
sadly to say there is no method present to return items with a wildcard, also redis don't support namespaces for stored items.
you need to define each item you want to receive, maybe you should look at a implementation like this
$receiveRedisKeys = [];
foreach($resultSet as $result)
{
$receiveRedisKeys[] = 'predefined_prefix_' . $result->getId();
}
$redisCacheResultSet = $redis->getItems($receiveRedisKeys);
i know that someone on github made a new repository where he modified redis to allow namespaces but this requires that you build the redis binarys by yourself from source. this leeds to a redis version you can't update anymore over apt-get
It's not possible, but there are some alternatives.
One idea is to keep a set with the keys you are interested in. That is the most common approach to this problem: each time you create one of the keys you will want to retrieve later, you add its name to a set. Then when you need to operate on one of those keys, you can grab it from the set. Read this article to get a general idea about this approach.
Another idea is to use the SCAN command to walk the keyspace with the pattern you are using, and as a second step retrieve the values with MGET followed by the keys you collected. This approach is good for administrative processes, but not as something that should be included in an application because the performance will be worse than that of the first idea. More about SCAN.
Finally, an option that is not recommended but I'm listing it just for completeness is to use the KEYS command to collect the keys you want, then proceed to get the values with MGET, as in the SCAN approach. This is not recommended as KEYS shouldn't be used in production environments. More about KEYS.

Write to config.groovy

I know it's possible to read in values from the grails-app/conf/Config.groovy but i was wondering if it is possible to write values as well?
something as simple as this doesn't seem to actually change the value in the Config.
def oldValue = grailsApplication.config.my.value
assert oldValue == "oldValue"
def newValue = "newValue"
grailsApplication.config.my.value = newValue
assert newValue == grailsApplication.config.my.value
I would like to use this as a way to store some values outside of a database without having to load up another properties file.
That's probably not going to be practical if I understand you correctly. You're really dealing with the compiled Config.class at runtime. Do you really want to check out Config.groovy from your VCS, modify it, check it back in, recompile it, and mess around with the Classloader to reload it? The only way I have found to do this is by externalizing their properties a database or file and managing state at runtime to deal with updates.
I agree with proflux's comment. Config.groovy is not the right place to persist any data generated by your application, and it's a good thing that what you try doesn't work :)
I am very curious as to why you don't want to persist these values in a regular database (of whatever kind). There is, of course always the option of storing this in a file somewhere, the path of which you can configure in Config.groovy. But even that to me only seems marginally helpful.
Why not add a domain class along the lines of this:
class Setting{
String key
String value
static constraints = {
key(unique: true)
}
}
This will probably be the easiest way of achieving what you're looking for, from what I can tell from here. But again, you should elaborate on what kind of data it is that you need to persist...

need session variables in valueUnbound?

I am using valueUnbound method of HttpSessionBindingListener to release lock(an entry from the database), before session is about to expire:
#Override
public void valueUnbound(HttpSessionBindingEvent event) {
String user = (String) event.getSession().getAttribute("currentUsr");
removeLock(user);
}
When the lock is set, I am setting up the username as a session variable.
I need this "username" in my remove lock method. But the getAttribute is throwing an exception:
java.lang.IllegalStateException: getAttribute: Session already invalidated
I need help in getting the session variable?? or is there any other way to get the username?
No, since session has been invalidated.
Although, I figured out the solution, I am setting the attribute via servlet context in
valueBound method and getting it through the : event.getSession().getServletContext().getAttribute("cUser");
it works fine. Thank You EJP
I got your point EJP, you are right , I am making it complex, I can get it from event.getValue() . +1 to your answer, Thank You.
Although, I figured out the solution, I am setting the attribute via servlet context in valueBound method and getting it through the : event.getSession().getServletContext().getAttribute("cUser");
So.. You are storing session scoped data in the application scope. Do you realize that this way the data is shared among all visitors of the webapp? Visitor X would then see the attribute set by visitor Y which has visited the website at a later moment. It makes the problem only worse.
Anyway, as to the concrete problem, as the exception message is trying to tell you, the session has already been invalidated at that point. There are two ways to solve this:
Make currentUsr a property of the class which is implementing HttpSessionBindingListener, so that you don't need to grab it as a distinct session attribute.
Use a HttpSessionListener instead. The sessionDestroyed() method is called right before invalidation, so you should still have access to all attributes.

Request variable in dotcms

Please find my requirement that needs to be done in dotcms-
1) I need to create a Map object for each request.
2) Across different widgets or containers i should be able to add data or get data from the Map object.
Can you please provide me a solution or idea to fulfill the requirement.
Thanks in advance
You can use the standard HTTPServletRequest methods, even from velocity:
$request.setAttribute("foo", "bar")
$request.getAttribute("foo") would print "bar"
$request.getParameter("foo") would get a GET or POST parameter "foo" and
#set($map = ${request.getParameterMap()}) would get the whole map.

Resources