How can I store raw json string in redis cache using laravel without php serialization? - laravel

When I cache json string like so cache()->rememberForever('globals', fn() => json_encode(['foo' => 'bar']));.
The value stored in cache is actually "s:13:\"{\"foo\":\"bar\"}\";" rather than "{\"foo\":\"bar\"}"?
Is there a way I can store string without php serialisation?

You would need to use the true cache storage like Redis::put(...). The cache facade(s) have a pretty helpful way of getting complex data in and out of cache. For instance you can cache models or associative arrays thru that facade and not worry about how it gets stringified behind the scenes. However, if you don't want that kind of helper/handling to cache and restore your variables - then use the caching storage directly.

You should be able to store the string using the Cache::forever function. From Laravel docs Storing Items Forever:
The forever method may be used to store an item in the cache permanently. Since these items will not expire, they must be manually removed from the cache using the forget method:
Cache::forever('key', 'value');
Given that, I would change your code to something like the following:
cache()->forever('globals', json_encode(['foo' => 'bar']));

Related

How to use user data in typo3 flux template without caching it

I try to get user session data in a typo3 flux content element. with {user.first_name} I can access the first name of the user, but this will get cached, meaning alls users will see the name of the first one accessing the page. How can I uncache that or load user session data in this template.
What I already tried:
<f:cache.disable> </f:cache.disable> unfortunately the user variable
are still cached...
<v:render.uncache> To make the user session data
accessible in the partial I need to pass it as a parameter, but the
parameters do get cached :(
<f:security.ifAuthenticated> does only
check for permission, but caches aswell.
Working methods:
adding config.no_cache = 1 or page.config.no_cache = 1 in the typoscript setup works, but I would like to use a solution in flux without typoscript, the USER_INT equival solution should be v:render.uncache, but the argument to be passed is cached as explained above
Thanks for any help
You have to use <v:render.uncache> and fetch the user session data inside the partial.
See https://github.com/FluidTYPO3/vhs/issues/1705

Using laravels {{old}} on dynamically created inputs

I have a form which allows a user to create an unlimited number of fields. If this forms fails validation I want the user to return to this page with the form populated with their previous input values - i.e. I want these fields to persist.
With a normal form I could do this with {{ old 'title' }}, however, these additional fields are being generated through JavaScript and so I cannot add this PHP snippet. What is the best way for me to retrieve these previous input values?
3 ways to do this, cache, sessions and cookies.
cache and sessions are server side which is much better for security, however it will take extra time and effort for setting up, but if the data is not sensible and can be passed within cookies, better to the cookies.
The best thing about cookies for your current situation is: you can set it up directly from your front end JS code.

What is the purpose of API Resources?

I've read the docs and watched the Laracast. I'm still left wondering why you would use them?
I get that you can map different data to different names if your field names were to change but you want to keep a consistent public API. But surly you can just do the same on the model with the toArray() method and change the mappings there?
If I were to do:
return User::find(1);
I get a response like:
{"id":1,"name":"Ova Parker"}
If I do:
return new UserResource(User::find(1));
I get a response like:
{"data":{"id":1,"name":"Ova Parker"}}
Is there a significance in wrapping it with a data tag? I am just guessing but is this a standard JSON format for API's? Why would you not just do return User::find(1); instead of using an API resource, if this is under API routes then it'll return it as JSON anyway automatically.
You kind of answer the question by yourself. The idea behind API Resources or Transformers (like the ones from Fractal) is to hide the db field names from the client. With return User::find(1) you expose your whole db structure which might not a good idea security-wise and also bad for the release process. If you need to change a db field name, you have to change the API too. With Resources you have a mapping between your application and the consumer of you API.
It seems more work in the beginning, but once you started it, you won't wanna miss it anymore.
There is no toArray() method in PHP, which gets magically called like __toString(). You have to write you own and call it by yourself. Resources are built-in by Laravel and will be resolved automatically.

Clear Magento cache based on key pattern

Is there a way in magento to clear specific parts of the cache whihc begin with a specific cache key.
I would like to clear everything in my module that have a key that begin with:
mynamespace_mymodule_
so the following will be clear from cache:
mynamespace_mymodule_asdaqasd
mynamespace_mymodule_qeqweq
mynamespace_mymodule_poipoi
etc
etc
Magento's caching system allows you to assign both cache key (unique id) and cache tags while saving your cache. Look at Mage_Core_Model_Cache::save method - 3d argument is cache tags array.
By assigning cache tags you'll have ability to remove all cache which is tagged with your cache tag at once. Mage_Core_Model_Cache::clean methid will take array of cache tags as an argument.

Using Isset with native sessions in CodeIgniter

I've read about how CI handles sessions differently than native sessions and feel slightly insecure about storing all the data in a cookie(?), unlike PHP's native session which only stores the session ID(?). So I've decided to use native sessions without the CI native_session library.
Now, I know that the Input Class in CI validates Isset with a true/false statement like this:
if ($this->input->post('something'))
which renders the Isset function unable to work (it gives an error). However, I'd like to check my native sessions with the Isset function, so how can I do this? I've tried
if (isset($_SESSION['keyHere']))
which gives me an error.
So to sum up: I want to use Isset on my session array as I feel using
if ($_SESSION['keyHere'])
without the Isset might be 'dangerous/foolish'.
Also, as a last question I'm curious about which session handling you think is safest? CI's Session Class or PHP's native handling with server-side storage etc.? I'd very much like to feel as safe as possible when it comes to sessions, no matter if it means I'll have to write longer code.
Ok so, lets talk about the problem that you're having with isset!
i agree to all answers behind, the empty function its an good try, using array_key_exists too, but isset have your own place on php language.
At first you didn't post the error taht you're getting, bu as #GolezTrol saidyou're probally having trouble with session_start().
You need to put it on the top of the page, before any scripts. I would put this code before all scripts, and the best place to put it its inside the config/config.php page, at the start of it, or maybe on the root index.php
Using it, you will starting the session on all pages of your system.
Well, about your second question.
I think the session native of PHP its really secure, but the session of Codeigniter it's really nice too.
Codeigniter has some problems with the security, when we talk about cookies, if we are handling with important data of system, its hard to work with it, especially if someone else could edit it, thinking about all of it that i describe above, i could say that Codeigniter has a good seession library, even if we are working with important data.
If you don't believe on the security of cookie, just put on database, and you will have any trouble with it. The good thing its the ability and the facility to work with this class on everyplace of the system!
Actually i'm using the way of database, and i suggest it.
I would use array_key_exists instead of isset. isset also checks if the value is not null, so an array with an existing key but a value of null will still return false. You know it is an array and you want to check if a key exists, so it makes the most sense to use array_key_exists.
But that's not the problem. :D
I think you need to call session_start() first.
PHP's native session handling performs just fine as long as you are on a single webserver.
$data=$this->session->userdata("session variable");
if($data)
{
// do something
}
use this in your CI code files. it works for me all the time !
$logindata = $this->session->userdata('username');
if ($logindata !== FALSE)
{
//it exists
}
might help!
I've always used php function empty(); though it works differently if you don't know what you are doing.
If it's an empty array, it will
return FALSE
If it's NULL, it will return FALSE
I personally had little problems with the new session class in CI 2.0.2. Create a table called "sessions" and store the sessions in the database, using sess_use_database to true. Also set sess_encrypt_cookies to true, and lastly, if you want sessions to match with user IPs, use sess_match_ip. Oh, and make sure to set encryption key, which will make it even more secure.
PHP sessions are nice, but I personally like CI sessions better because it gives more flexibility. Especially when you are running multiple web heads with load balancer.
Hope that helps!
Why not use the built in CodeIgniter Session Class? it will provide the same functionality as the input class in regards to not forcing you to use isset().
It allows for encrypted sessions and uses it's own session implementation.
$this->session->userdata("keyHere"); // returns false if not set
You really should use the codeigniter session class. You can make it store the session data in the db so the cookie holds just a unique identifier. You can also set that cookie to be encrypted with the codeigniter security key.
Then, when you access session information, it acts like the normal input methods and so you can just do if($this->session->userdata('name')) rather than using isset or empty or whatever.

Resources