equivalent function for $this->_ci_cached_vars in CI Helpers - codeigniter

I'm creating a custom helper in codeigniter. There is an instance where I check if certain parameter is passed to view.
In view, i can get all the passed variables by using this function:
$this->_ci_cached_vars
but it returns blank when used in the custom helper.
Is there any equivalent function of this that can be used in the helper?
Thanks in advance.

_ci_cached_vars is a property of the Loader class. So something like this should work (untested):
$CI =& get_instance();
$vars = $CI->load->_ci_cached_vars;

You can use $GLOBALS['CI']->load->get_var('your_key_here') tested in CI 2.1.2

I'm not sure if older versions of CodeIgniter support this, but in the v3 release the Loader class has a public method get_vars() that allows you to read the value of _ci_cached_vars.
Although this question is very old it's the first hit on Google that I found while searching for this problem. I hope this post helps someone out who follows the same path on Google as I did! :)

Related

Are there laravel method like in_array?

I try to use methods in https://laravel.com/docs/9.x/helpers instead of old php functions I used for many
year. I did not find any laravel method like in_array.
I tried to use Str::contains , but failed it. Seems different ?
So any replacement of in_array ?
Thanks in advance!
Using in_array seems perfectly acceptable and if it gets the job done for you and is not causing issues then stick with it.
That aside, there is the Arr::has() helper, or alternatively, you could convert your array to a collection and then use the contains() method, however that just uses in_array behind the scenes anyway.

How can I render a twig template in a custom controller in Silex?

I'm playing with Silex microframework to build a very simple app.
The Silex documentation briefly illustrates how to keep your code organised using controller as class and I've also found this useful article talking about the same practice:
https://igor.io/2012/11/09/scaling-silex.html
but still can't solve my problem
The issue:
in my app.php I'm using
$app->get('/{artist}', 'MyNamespace\\MyController::getAlbum');
This is working. MyController is a class correctly loaded through composer using psr-4.
At the moment the return method of getAlbum($artist) is return $player;
What I'd like to do instead, is returning a twig view from getAlbum, something like:
return $app['twig']->render('player.twig', $player);
To do so, what I've tried to do in my custom class/controller is:
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
[...]
public function getAlbum(Request $request, Application $app, $artist)
but this is generating the following error when I try to access the routed pages:
ReflectionException in ControllerResolver.php line 43:
Class MyNamespace\Request does not exist
Whic made me think that there's a namespace conflict between myNamespace and the Silex namespaces?!
What am I doing wrong?
Is this the right way to make $app visible in my custom controller in order to use return $app['twig']... ?
Thank you in advance!
EDIT
After several other tries still didn't get to the point (replies still welcome!) but I've found a workaround solution that could be useful to anyone will incur in a similar issue. Directly in my app.php I added this
$app->get('/{artist}', function (Silex\Application $app, $artist) use ($app)
{
$object = new MyNamespace\MyController();
$player = $object->getAlbum($artist);
return $app['twig']->render('player.twig',
array(
//passing my custom method return to twig
'player' => $player,));
});
then in my player.twig I added:
{{player | raw}}
And this basically means that I still need an anonymous function to get use of my custom method which is a working solution I'm not really happy with because:
I'm using 2 functions for 1 purpose.
The return value of getAlbum is dependent from the use of "raw" in twig.
SOLVED
The workflow described works fine. It was a distraction error: I've placed the namespace of my custom class after use Symfony\Component\HttpFoundation\Request;
namespace declaration in PHP needs always to be at the top of the file, Silex wasn't able to injects $app and $request for this reason.

Joomla - call function model within the model

In a public function of my model I call
$user_type=$this->get_user_type();
In the same model I have
private function get_user_type()
{
$user_type='asd';
$asd_groups = (int)$config->get('asd_groups');
$ver_groups = (int)$config->get('ver_groups');
jimport( 'joomla.user.helper' );
$user_groups=JUserHelper::getUserGroups($user->id);
if(in_array($asd_groups,$user_groups)){
$user_type='asd';
}
if(in_array($ver_groups,$user_groups)){
$user_type='ver';
}
return $user_type;
}
The site give me a white page, if I comment the calling line "$this->get_user_type();" then it works...
I really don't understand what is wrong here.
There is not enough information or code here to help you… for example where is $config coming from and what is it? What version of Joomla is this on?
If $config is not defined as a global then that may be the source of the problem depending on your PHP setup.
Things you can do to help yourself find the problem, in Joomla's Global Configuration.
Set Error Messages to "Development" in Joomla (you are using a development site and not a live website right?)
Turn on Joomla's DEBUG mode
Then update your question with details of error messages, Joomla version and where this code is running (you say your model) and where $config is coming from.
Oh sure!
I have missed the two configuration variable when i moved the code from inside a function in a dedicated function.
I copied these two lines on the first row of the function and now it works!
$config = JComponentHelper::getParams(S_APP_NAME);
$user = JFactory::getUser ();

Debugging Grocery_CRUD Callbacks

I have seen many people referring to the usage of call_user_func() to debug the problems in a Grocery_CRUD callback, but unfortunately no one has come off with a complete example to actually how to use it like where to to place a call to the test function [just_a_test()] in the controller an example of what I am trying to discover is here.
I am unable to understand where do we call this
just_a_test(),
how are we able to pass on the desired parameters using call_user_func(array($this,'insert_coupon_codes')); when there are no para's being passed to the just_a_test()?
how come the insert_coupon_codes will be able to get the desired para's?
Grocery CRUD add the parameters automatically from the library. You are not able (till now at version 1.1.8) to add more parameters at your callback.
Update: At the latest version of Grocery CRUD now you CAN pass as many parameters as you need. This is a functionality that PHP is offering from version PHP 5.4 or later. More specifically with the use keyword. More specifically if you have the callback callback_after_insert:
usually you would use it like this:
$crud->callback_after_insert(function ($post_array,$primary_key) {
// Your code here
});
From PHP 5.4 version and later you can add more parameters with the use so for example you could have:
$my_variable = 'test';
$another_variable = 'hello';
$crud->callback_after_insert(function ($post_array,$primary_key) use ($my_variable, $another_variable) {
// Now you can use the variables $my_variable and $another_variable at your callback
});

get magento module config data in Observer

I created a module with an observer for the sales module with event hook ‘sales_order_shipment_save_after’ ,
My module has the following files
Company/Modulename/etc/config.xml
Company/Modulename/etc/system.xml
Company/Modulename/Model/Observer.php
there are four fields in the modules admin configuration fields
I want to get those saved data in the Observer class.
using $this->getConfigData(’password’); gives a
Call to undefined method
error
Any suggestions?
Magento uses a static method on the global Mage application object to get configuration values
$config = Mage::getStoreConfig('section_name/group/field'); //value
$config = Mage::getStoreConfig('section_name/group'); //array
An amendment to Alan's completely correct answer.
Along with path as first parameter, getStoreConfig also accepts storeid as second parameter(optional).
Well, this is useful when you want to retrieve store-wise values.
Alan has mentioned this point in his own tutorial. I guess, he has not mentioned here just because OP has not mentioned this requirement in his question.
Please refer this
In a shipment module I can use $this->getConfigData for fields in system.xml, but in another kind of modules sometimes not, e.g. extends Mage_Core_Model_Abstract, than I must use getStoreConfig. So the answer is you don't have to use always getStoreConfig. But I don't know why ...
Answer: getConfigData is just defined in a shipment class and uses getStoreConfig too. A little confusing that some functions are extra defined and unneeded in fact ...

Resources