$arr = array();
function test($status){
global $arr;
array_push($arr,$status);
}
How can I declare $arr as global array.
To set global variable in Yii use:
Yii::app()->params['arr'] = array();
to get variable:
echo Yii::app()->params['arr'];
Or you can place them in the main.php config file for each application.
Related
Is there a way to evaluate a variable in a blade directive? I try to get the value of an associative array in function of dynamically passed arguments over directive
Having the following will fail
Blade::directive('imagepol', function ($expression) {
$options = get_field('fallback_image', 'option');
if(!empty($expression) && $options) {
return "<?= isset($options[$expression]) ? wp_get_attachment_image_srcset($options[$expression]['ID']) : ''?>";
}
});
I call the directive as it follows
#imagepol($item->get('pattern_options'))
In this case will fail with the following
Undefined array key "$item->get('pattern_options')"
In Laravel 5.1 TestCase, the baseUrl is hard-coded. I'd like to set it based on the value I have set in .env.
How do I access the .env variables within the TestCase class?
in Laravel 5.0 TestCase, I can get .env variable with following function.
getenv('APP_VARIABLE');
I think it should work with Laravel 5.1 as well as getenv() is a PHP function.
Start Dotenv to get .env variables in the TestCase stage
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();
Dotenv::load(__DIR__.'/../');
$this->baseUrl = env('APP_URL', $this->baseUrl);
return $app;
}
I can confirm that Christopher Raymonds suggestion above, replacing the
Dotenv::load call
with
this $app->loadEnvironmentFrom('.env.testing');
works with Laravel 5.4
See Example:
protected $baseUrl = 'http://localhost';
/**
* Creates the application.
*
* #return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
$app->loadEnvironmentFrom('.env');
$this->baseUrl = env('APP_URL', $this->baseUrl);
return $app;
}
I have this in my .env file:
APP_URL=http://project.dev
I then modified the createApplication function in tests/TestCase.php
/**
* Creates the application.
*
* #return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$this->baseUrl = env('APP_URL', $this->baseUrl);
$app = require __DIR__ . '/../bootstrap/app.php';
$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
return $app;
}
Turned out I needed to add the APP_URL key to my phpunit.xml file.
For whatever reason, I thought some .env file would also be loaded in the process but apparently that's not the case.
I modified the createApplication function in tests/TestCase.php adding this lines after bootstrap ...->bootstrap();
$env = new Dotenv\Dotenv(dirname(__DIR__), '../.env');
$env->load();
In Laravel 5.2 I replaced the Dotenv::load call with this ...
$app->loadEnvironmentFrom('.env.testing');
I should change on zf2 controller a variable global. In practice, I'd like a function to insert the value in the global variable and another function prints the value of the variable. for example:
protected $variablename;
public function setAction()
{
$this->variablename = "hi friends!";
}
public function getAction()
{
var_dump($this->variablename);
}
In this example, when I print the variable I always get NULL.
Any suggestions? tnx
simone
indirectly you can't because of the lifecircle in a zend/php request. after you set up your variable in actionA the variable is reseted if you request actionB
if you want to set your variabel data in actionA and make the data present in actionB you need a persistent save storage (like a db, cookie or session. otherwise it is only possible to forward the current request to another request in your controller. then have a look at the forward controller helper in zend.
http://framework.zend.com/manual/2.3/en/modules/zend.mvc.plugins.html#zend-mvc-controller-plugins-forward
//edit after comment
you can use a session in zend like this
set session
$session = new Zend\Session\Container('base');
$session->offsetSet('someSettings', 'someValue');
get session
$session = new Zend\Session\Container('base');
if( $session->offsetExists('someSettings') )
{
$someSettingValue = $session->offsetGet('someSettings');
}
Prestashop
I am stuck we one problem for cookie. In prestashop 1.4.7 I create a custom cookie variable using setcookie but when i am try to access and assign it on front-controller, I am not getting cookies set value.
here is my script:
page: checkpostcode.php
include(dirname(__FILE__).'/config/config.inc.php');
include(dirname(__FILE__).'/init.php');
global $cookie;
setcookie("is_postcode_checked", 1, time()+600, "/", "", 1); // Set the cookie in basepath
On frontcontroller.php page :
I am access it using $_COOKIE and assign it into smarty array.
'is_postcode_checked' => $_COOKIE['is_postcode_checked'] // Getting null value for cookie
page: checkpostcode.tpl
{$cookie->_get(postcode_checked_msg)} // here get the is_postcode_checked value but
but I am not able to get is_postcode_checked variable value.
In prestashop 1.5, global are deprecated.
To set something in the cookie :
In a controller :
$this->context->cookie->__set($key,$value);
Other file :
$context = Context::getContext();
$context->cookie->__set($finger_print,$result);
You can access to your value with :
In a controller
$this->context->cookie->key
Other file :
$context = Context::getContext();
$context->cookie->key;
You should use Prestashop's own cookie class entirely rather than using the PHP setcookie() function. The class uses the "magic methods" __get(), __set(), __unset() and __isset() which should allow you to do this easily.
Try in your "page" code (not sure how you're executing this since it doesn;t look like an additional page controller):
global $cookie;
$cookie->is_postcode_checked = 1;
$cookie->write(); // I think you'll need this as it doesn't automatically save
...
And in your FrontController override:
global $cookie;
if (isset($cookie->is_postcode_checked))
$is_postcode_checked = $cookie->is_postcode_checked;
else
$is_postcode_checked = 0;
You can assign the variable $is_postcode_checked to a corresponding smarty variable to use in your template.
If you want to fetch the cookie from Prestashop cookie class, you should store it in this class too.
Use die() function in your controller, to find out is that cookie set
It is better, as Paul said, to use only global $cookie class to store and getting data.
My question maybe is simple, but anfortunatly I don't know how to answer it.
How can I create a variable in a hook function and then pass a PHP variable from within the function to my Template in order to use it in the form of {VARIABLE_COMES_FROM_HOOK}
WHMCS uses Smarty template engine so as an example inside the includes/hooks folder you will have your hook file e.g hook_example.php from which you can assign variables to $smarty global object and access them in your template as {$VARIABLE_COMES_FROM_HOOK}.
Inside hook_example.php:
function example() {
global $smarty;
$variable = "Hello world!";
$smarty->assign('VARIABLE_COMES_FROM_HOOK', $variable);
}
Search in hook categories http://docs.whmcs.com/Hooks
There are hooks that return variables to tpl and other that don't do that