how to reference a session variable within a file outside of Controller, Model or View? - phalcon

I set a session variable inside a controller :
$this->session->set("navig_param",$livc_code);
Now I want to test if that session variable is not empty inside a php file outside of the Controller domain, Model domain and View domain. In fact this file is in a particular folder named lib at the same level as controllers, models and views. So how to test the session variable in this php file ?

You can also access your session data ( statically ) via
$session = Phalcon\Di::getDefault()->get('session');
$navigParam = $session->get('navig_param');
or oneliner:
$navigParam = Phalcon\Di::getDefault()->get('session')->get('navig_param');
reference : phalcon static DI

$di=\Phalcon\DI::getDefault();
$session=$di->getSession();
if($session->has("navig_param"))
{
$navig_param=$session->get("navig_param");
//do something with it like flash it:
$di->getFlash()->success('navig_param is: '.$navig_param);
}

As session is a service you only need an access to DI container:
$navigParam = $di->get('session')->get('navig_param');
Alternatively you can always use $_SESSION super global:
$navigParam = $_SESSION['navig_param'];
To get $di you can use: $di = DI::getDefault();

Related

How to access services from a view script in Zend Framework 3?

I have a custom authentication service and in ZF2 I accessed this as follows:
Application/view/layout/layout.phtml
$authenticationService = $this->getHelperPluginManager()
->getServiceLocator()
->get('AuthenticationService');
$currentIdentity = $authenticationService->getIdentity();
Now the Zend\ServiceManager#getServiceLocator() is deprecated.
How to get a service available in a view script (or concrete in this case in the layout) in ZF3?
For this purpose there is already Identity View Helper
As documentation says
// Use it any .phtml file
// return user array you set in AuthenticationService or null
$user = $this->identity();
The solution is to assign a global view variable in the onBootstrap(...):
namespace Application;
use ...
class Module
{
public function onBootstrap(MvcEvent $e)
{
...
$serviceManager = $e->getApplication()->getServiceManager();
$viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
$viewModel->authenticationService = $serviceManager->get('AuthenticationService');
}
...
}
Another (perhaps an even better/cleaner) solution is to use a ViewHelper. See also here.

How to set Attribute to PDO connection in Codeigniter

How to set attributes (PDO::ATTR_ERRMODE) on the PDO database handle in Codeigniter?
I think a better option is to use a MY_Model (which you then extend and this is available then across the application) and define something like this in the construct:
$this->db->conn_id->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
Note conn_id allows you to access the main PDO object.
There are two ways:
1. The lazy (hacky) way
Add to the following code into system/core/database/drivers/pdo/pdo_driver.php (in CI 3):
public function db_connect($persistent = FALSE)
{
$this->options[PDO::ATTR_PERSISTENT] = $persistent;
// Added code start
$this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
// Added code end
try
{
return new PDO($this->dsn, $this->username, $this->password, $this->options);
...
}
2. The right way
Extend Database Driver and add the same line
Note: If you will set PDO::ERRMODE_EXCEPTION in Codeigniter it will show exception errors even in Production environment.

pass global variable between two function on zf2 controller

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');
}

Custom Cookie variable + Prestashop

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.

dynamic file path in log4php

I am new to log4php.
I would like to save the log files in the format /logs/UserId/Info_ddmmyyyy.php
where the UserId is dynamic data.
(I would basically like to save one log per user.)
Is there any way to change the log file path dynamically?
This behaviour is not supported by default. But you can extend LoggerAppenderFile (or RollingFile, DailyFile whatever your preference is) to support it.
Create your own class for that and make it load to your script.
Then extend from this class:
http://svn.apache.org/repos/asf/logging/log4php/trunk/src/main/php/appenders/LoggerAppenderFile.php
class MyAppender extends LoggerAppenderFile { ... }
You'll need to overwrite the setFile() method, similar to:
public function setFile($file) {
$path = getYourFullPath();
$this->file = $path.$file;
}
After all you need to use your new Appender in you config
log4php.appender.myAppender = MyAppender
log4php.appender.myAppender.layout = LoggerLayoutSimple
log4php.appender.myAppender.file = my.log
Please note, instead of giving your full path to the log file you now need to add a plain name. The full path (including username) must be calculated with your getYourFullPath() method.
Hope that helps!
Christian