So one of my questions is there a way to set fetching data from the database as default fetchMode such as FETCH_LAZY or FETCH_OBJ without using the setFetchMode() method each time a statement is executed.
Also I am using the session container to save the data of the available languages coming from my database, and using it as a namespace like FrontEnd in this case, now i have checked in the session component Container class that you have a method to check if the Namespace exists, previously in the Zend Framework I was using the registry method to save and check them if they exist with static methods, my question here is there a way to get the saved session data or should I check the Namespace each time in different actions in modules?
So my next step is to make on my application visit that if no language in route is set will redirect the user to default language in this case EN as i have the following in my Application Module:
'router' => [
'routes' => [
'app' => [
'type' => Segment::class,
'options' => [
'route' => '/[:lang]',
'constraints' => [
'lang' => '[a-z]{2}'
],
'defaults' => [
'lang' => 'en',
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'plugin' => [
'type' => Literal::class,
'options' => [
'route' => '/plugin',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'plugin',
],
],
'may_terminate' => true,
],
],
],
],
],
Here is my Application class:
namespace Nanocord\Mvc;
// Load classes
use Laminas\Db\Adapter\Adapter;
use Laminas\Db\Sql\Sql;
use Laminas\Mvc\Application as LaminasApplication;
use Laminas\Session\Config\SessionConfig;
use Laminas\Session\Container;
use Laminas\Session\SessionManager;
use Laminas\Session\Storage\SessionArrayStorage;
use Laminas\Session\Validator\HttpUserAgent;
use Laminas\Session\Validator\RemoteAddr;
/**
* Application
*
* @category Nanocord
* @package Mvc
* @subpackage Application
*/
class Application extends LaminasApplication
{
private $config;
private $db;
private $dbIsConnected;
private $session;
public function __construct($config)
{
$this->config = $config;
}
public function start()
{
$application = parent::init($this->config)->run();
$db_config = $application->getConfig()['db'];
$db_adapter = new Adapter($db_config);
$db_adapter->driver->getConnection()->connect();
if ($db_adapter->driver->getConnection()->isConnected() === true) {
$this->db = $db_adapter;
$this->dbIsConnected = true;
$this->initSession();
$this->initLanguage();
}
}
public function initSession()
{
$session_config = new SessionConfig();
$session_storage = new SessionArrayStorage();
$session_manager = new SessionManager($session_config, $session_storage, null, [HttpUserAgent::class, RemoteAddr::class]);
$session_manager->getValidatorChain()
->attach('session.validate', [new HttpUserAgent(), 'isValid']);
$session_manager->getValidatorChain()
->attach('session.validate', [new RemoteAddr(), 'isValid']);
$session = new Container('FrontEnd', $session_manager);
$this->session = $session;
//echo '<pre>' . var_export($session, true) . '</pre>';
}
public function initLanguage()
{
$statement = $this->db->createStatement('SELECT * FROM application_language WHERE active = ?', [1]);
$languages = $statement->execute();
//$languages->setFetchMode(1);
$languages_array = array();
$languages_default = null;
foreach($languages as $language) {
$languages_array[] = $language['code'];
if ($language['predefined'] == 1) {
$languages_default = $language['code'];
}
}
$this->session->languages_default = $languages_default;
$this->session->languages_available = $languages_array;
var_dump($this->session->languages_default);
var_dump($this->session->languages_available);
}
}