zend Framework. Calling a different javascript file specified by action/controller - zend-framework

I want to split a unique javascript file to get a file by action.
On my controllers I use jQuery()->addJavascriptFile() to call js files :
class DemandesController extends Zend_Controller_Action
{
public function init()
{
$this->view->jQuery()->addJavascriptFile('public/js/' . $this->getRequest()->getControllerName() . '/' . $this->getRequest()->getActionName() . '.js');
}
}
In my public/js folder I got a folder by controller and a js file by action...
It works well only with the indexAction, for all of the others the name of the controller is inserted into the URI :
With indexAction : http://192.168.78.208/demande_absence/public/js/demandes/index.js
With any other : http://192.168.78.208/demande_absence/demandes/public/js/demandes/nouvelle.js
What I've done wrong ?

Try:
$this->view->jQuery()->addJavascriptFile('/public/js/' . $this->getRequest()->getControllerName() . '/' . $this->getRequest()->getActionName() . '.js');
And i think you also have to use something like the baseurl view helper to build a correct url.

Maybe because it's not an absolute path. Try this:
$this->view->jQuery()->addJavascriptFile('/public/js/' . $this->getRequest()->getControllerName() . '/' . $this->getRequest()->getActionName() . '.js');

Related

zend session namespace not working

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

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

How to include js and css in Zend Form Elements?

I create my own zend form element - App_Form_Element_Wysiwyg
<?php
class App_Form_Element_Wysiwyg extends Zend_Form_Element_Xhtml
{
/**
* Default form view helper to use for rendering
* #var string
*/
public $helper = 'wysiwyg';
}
Then I create helper for wysiwyg - App_View_Helper_Wysiwyg
<?php
class App_View_Helper_Wysiwyg extends Zend_View_Helper_FormTextarea
{
public function wysiwyg($name, $value = null, $attribs = null)
{
// Here I want to include js and css for wysiwyg editor
...
$xhtml = '<textarea name="' . $this->view->escape($name) . '"'
. ' id="' . $this->view->escape($id) . '"'
. $disabled
. $this->_htmlAttribs($attribs) . '>'
. $this->view->escape($value) . '</textarea>';
return $xhtml;
}
}
How can I include js and css files for wysiwyg editor?
The headScript() helper lets you add or manipulate script tags in the header. So within your helper you can do:
$this->view->headScript()->appendFile('/path/to/file.js');
then just ensure your layout is calling the helper somewhere in the head section:
<?=$this->headScript()?>
to output the HTML. There is also the headStyle() helper which works in a similar way and can be used for your CSS.
Relevant docs:
HeadScript
HeadStyle

Joomla setRedirect doesn't work

I have a simple Joomla controller, but I can't redirect anything.
According to the documentation:
class MyController extends MyBaseController {
function import() {
$link = JRoute::_('index.php?option=com_foo&ctrl=bar');
$this->setRedirect($link);
}
}
//The url contains & html escaped character instead of "&"
This should work, but I get a malformed URL. Is there something I'm missing here? Why is Joomla converting all the "&" characters into &'s? How am I suppose to use setRedirect?
Thank you
Alright, I fixed it. So if anyone needs it:
instead of
$link = JRoute::_('index.php?option=com_foo&ctrl=bar');
$this->setRedirect($link);
use
$link = JRoute::_('index.php?option=com_foo&ctrl=bar',false);
$this->setRedirect($link);
to make it work.
Glad you found your answer, and by the way, the boolean parameter in JRoute::_() is by default true, and useful for xml compliance. What it does is that inside the static method, it uses the htmlspecialchars php function like this: $url = htmlspecialchars($url) to replace the & for xml.
Try this.
$mainframe = &JFactory::getApplication();
$mainframe->redirect(JURI::root()."index.php?option=com_foo&ctrl=bar","your custom message[optional]","message type[optional- warning,error,information etc]");
After inspecting the Joomla source you can quickly see why this is happening:
if (headers_sent())
{
echo "<script>document.location.href='" . htmlspecialchars($url) . "';</script>\n";
}
else
{
... ... ...
The problem is that your page has probably already output some data (via echo or some other means).
In this situation, Joomla is programmed to use a simple javascript redirect. However, in this javascript redirect it has htmlspecialchars() applied to the URL.
A simple solution is to just not use Joomlas function and directly write the javascript in a way that makes more sense:
echo "<script>document.location.href='" . $url . "';</script>\n";
This works for me :)
/libraries/joomla/application/application.php
Find line 400
// If the headers have been sent, then we cannot send an additional location header
// so we will output a javascript redirect statement.
if (headers_sent())
{
echo "<script>document.location.href='" . htmlspecialchars($url) . "';</script>\n";
}
replace to
// If the headers have been sent, then we cannot send an additional location header
// so we will output a javascript redirect statement.
if (headers_sent())
{
echo "<script>document.location.href='" . $url . "';</script>\n";
}
This works!

How can I forward a public path to another Catalyst controller?

Let say the public URL /faq is linked to the private path /faq/index in my Catalyst application:
package MyApp::Controller:FAQ;
sub index :Path {
[....]
How can I do a forward to /faq from another controller, that is how can I know that the action for the URL /faq is /faq/index ? Something like:
$c->forward(c->dispatcher->get_action_by_path( "/faq" )); # does not work
I got the answer for the Catalyst mailing list:
my $path = "/" . join '/', #{$c->req->args};
$c->request->path($path);
$c->dispatcher->prepare_action($c);
$c->detach($c->action, $c->req->args);