zend session namespace not working - zend-framework

I am unable to access zend session in included file via layout.
what i have done so far -
//bootstrap
public function _initSession()
{
Zend_Session::start();
$test = new Zend_Session_Namespace('test');
}
//controller
public function init(){
$test = new Zend_Session_Namespace('test');
$test->abc = 'defghi';
}
//layout include file
<?php include_once( APPLICATION_PATH . '/data/ga_test.php');?>
//ga_test.php
$test = new Zend_Session_Namespace('test');
echo 'this is ' . $test->abc;
I am not able to access the variable in ga_test file. I am getting an empty variable. But if I include ga_test end of each view file then it works. Obviously I don't want to go to every view file and include ga_test.php. Can I do this via layout.
I am sure, I am doing something wrong here. Any help would be really appreciated.
Thanks

Related

PHP dynamic URL keeps adding/doubling the full path when i click a button on the site

I am running into an issue and I hope someone here can help. I have put together a dynamic URL manipulator that should just work out the path to the file being displayed, which it does and when I click a link, it should process the new link and replace the existing URL. The issue is that instead of replacing the URL with the new url path, it adds it to the end of the existing URL which is why it does not work. an example is as follows:
https://localhost/home/main/localhost/home/main/pages/about
the part from the second localhost... should replace the first url path but it adds to it and therefore it does not work. the code for this is as follows:
class Core {
protected $currentController = 'Pages';
protected $currentMethod = 'index';
protected $params = [];
public function __construct(){
//print_r($this->getUrl());
$url = $this->getUrl();
if(file_exists('../app/controllers/' . ucwords($url[0]). '.php')){
$this->currentController = ucwords($url[0]);
unset($url[0]);
}
require_once '../app/controllers/'. $this->currentController . '.php';
$this->currentController = new $this->currentController;
if(isset($url[1])){
if(method_exists($this->currentController, $url[1])){
$this->currentMethod = $url[1];
unset($url[1]);
}
}
$this->params = $url ? array_values($url) : [];
call_user_func_array([$this->currentController, $this->currentMethod], $this->params);
}
public function getUrl(){
if(isset($_GET['url'])){
$url = rtrim($_GET['url'], '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = explode('/', $url);
return $url;
}
}
}
Hi anyone who cares to know the solution to this, or have a similar issue and is looking to find a solution.
I found out that the issue was with the htaccess file which did not have the full root of the home page.
I had entered
RewriteBase /main/public
my app is in a subfolder called home so after updating it to the following, it works like a charm.
RewriteBase /home/main/public

Assigning View Helpers ZF 1.12

I can't get view helpers to work. This is what I've done so far:
I added this in application.ini
resources.view.helperPath.My_View_Helper = APPLICATION_PATH "/helpers"
In directory application/helpers I put the file findurl.php
Inside findurl.php I put
<?php
class My_View_Helper_findurl extends Zend_View_Helper_Abstract
{
public function findUrl($url){
$url = $url;
return htmlspecialchars($url);
}
}
?>
Then in a view.phtml file I tried $this->findurl("http://google.com"); and got no luck, page goes blank.
I know I'm probably getting naming conventions wrong, could anyone help me out? Thank you.
I also tried this in view.phtml
$helper = $this->_helper->getHelper('findurl');
echo $helper->findUrl("http://google.com");
Try:
$this->findurl('url');
http://framework.zend.com/manual/1.12/en/zend.view.helpers.html

Using Zend Framework 1 Service Window Azure Standalone

I am using zend service for window azure wrapper class standaline in my custom application. I would like to know how do i connect to my window azure storage. there seems to be no way i can specify the connection details (storage key etc)
$storageClient = new Zend_Service_WindowsAzure_Storage_Blob();
$result = $storageClient->createContainer('testcontainer');
echo 'Container name is: ' . $result->Name;
I am taking refernces from http://framework.zend.com/manual/1.12/en/zend.service.windowsazure.storage.blob.html
zend/Azure expert advice appreciated. thanks
Just add this code to the IndexController in your application
public function indexAction()
{
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$this->view->content = '<h1>Welcome to TestIndex!</h1>';
// Need to change
}
else
{
$storageClient = new Zend_Service_WindowsAzure_Storage_Blob('blob.core.windows.net', 'YOURNAME', 'YOURCODE');
$result = $storageClient->createContainer('container');
echo 'Container name is: ' . $result->Name;
var_dump($result->Name);
exit();
}
}

ZEND tralslations for addMultiOption text in Form for poEdit

I dont have an idea why translations are not working in with Zend_Form.
I would like to be able translate options for selects.
For now i have something like this:
my form class:
(...)
$this->translate = Zend_Registry::get('translate');
Zend_Form::setDefaultTranslator( Zend_Registry::get('translate') );
(...)
$select = new Zend_Form_Element_Select('select');
// $select->addMultiOption('0', $this->translate('Aktywny'));
$select->addMultiOption('0', $this->translate->_('Aktywny'));
$select->addMultiOption('1', 'Nieaktywny');
in my bootstrap file i have something like this:
protected function _initTranslate()
{
Zend_Loader::loadClass('Zend_Translate');
Zend_Loader::loadClass('Zend_Registry');
$translate = new Zend_Translate('gettext', APPLICATION_PATH.'/languages',
'browser',
array('scan' => Zend_Translate::LOCALE_FILENAME));
//changing language and setting it to session if changed
$session = new Zend_Session_Namespace('jezyk');
if(isset($session->language)) {
$translate->setLocale($session->language);
} else
$translate->setLocale('pl');
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Translate', $translate);
}
and it works fine for controllers, phtml files and plugins where i call it by
$this->translate('string to translate');
and in plugins
$this->view->translate('string to translate');
but those methods won't work in form. It throws exception:
Warning: Exception caught by form: No entry is registered for key 'translate' Stack Trace: #0
to make it working as i wrote in comment just have to change line:
$this->translate = Zend_Registry::get('translate');
for
$this->translate = Zend_Registry::get('Zend_Translate');
cause i didn't saw that i'm getting wrong translate from registry. It should be Zend_Translate like in Bootstrap file, not translate as i did.
And this is solution for my problems with translate and now i can make translations in form files :)

Zend Layout - A "Smart" layout selector

I currently have Zend setup to look for a layout script in each module's view/scripts/layout.phtml file (ie: /application/modules/moduleName/scripts/layout.phtml). This is by setting layout[] to nothing (blank) in the application.ini file (resources.layout[] = )
The issue is that many modules may share the same layout. I don't want to copy the same exact layout into each module that uses it. I know I can set everything to use one layout script by setting a specific path like resources.layout.layoutpath = /layoutPath and everything would use /layoutpath/layout.phtml, and I know I can set individual pages (or whole Controllers, in the init) by using $this->_helper->layout->setLayout('foobaz');
The issue is that some modules will have different layouts, other than the 'standard' one, and I don't want to set it on a by Controller or by Action basis. I want to set it for the entire module, set in one place (or intuitively figured out by code/Zend automatically). Ideally, it would be setup how it is currently, but if a module doesn't have its own layout.phtml, it would use the default module's layout.
So... how do I do it?
There are several solutions, choose their own strategy
1 extending the action controller
class App_Controller_Action extends Zend_Controller_Action
{
public function init()
{
parent::init();
$moduleName = $this->getRequest()->getModuleName();
$layoutPath = APPLICATION_PATH . '/modules/' . $moduleName . '/layouts';
if (is_dir($layoutPath)) {
$this->view->addScriptPath($layoutPath);
}
}
}
and then do as usual IndexController extends App_Controller_Action ...
if layout file exists in APPLICATION_PATH . '/modules/' . $moduleName . '/layouts' directory - it will ne used instead of default layout
2 you can write frontcontroller plugin
class App_Controller_Plugin_ModuleSwitcher extends Zend_Controller_Plugin_Abstract
{
protected $_view = null;
public function routeShutdown(Zend_Controller_Request_Abstract $request)
{
$moduleName = $request->getModuleName();
Zend_Layout::startMvc();
$layout = Zend_Layout::getMvcInstance();
$layout->setLayoutPath(APPLICATION_PATH . '/modules/' . $moduleName . '/layouts')->setLayout($moduleName);
return $request;
}
}
and dont forget to google for another solutions ;)
you can set own layout selector in few steps
step 1:
make module admin and default.
step 2:
create layout folder in each module as admin/layouts/scripts
and default/layouts/scripts
put into layout.phtml
step 3:
delete the layout.phtml file from Application/layouts/scripts.
step 4:
make the the Plugin folder inside library and make Plugin.php
as
class Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$layoutPath = APPLICATION_PATH . '/modules/' . $request->getModuleName() . '/layouts/scripts/';
Zend_Layout::getMvcInstance()->setLayoutPath($layoutPath);
}
}
step 5:
open Application/configs/Appication.ini file
and edit it
as
;resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.layout.layout = "layout"
;register your plugin
autoloaderNamespaces[] = "Plugin"
resources.frontController.plugins[] = "Plugin_Layout"
Step 6:
open bootstrap file Application/Bootstrap
put the code inside
protected function _initAutoload()
{
$loader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/modules/'
));
return $loader;
}
protected function _initPlugins()
{
$this->bootstrap('frontcontroller');
$fc = $this->getResource('frontcontroller');
$fc->registerPlugin(new Plugin_Layout());
}
The quickest solution might be to create a symlink to point what would be a module layout file to the default layout. This won't work on Windows and is harder to maintain.
Better, create a method in your Bootstrap to set the layout.
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initLayoutScript(){
//ensure layout is setup
$this->bootstrap(array('layout', 'FrontController'));
$layout= $this->getResource('layout');
$front = $this->getResource('FrontController');
//do something with $layout and $front - set layout script/path etc based on request
//You could use file_exists to detect module layout scripts
}
}
See http://framework.zend.com/manual/en/zend.application.quick-start.html#zend.application.quick-start.resources for more details.
Finally, you could write your own application resource for use with Zend_Application.