I have created a zend project on ubuntu which is in /var/www/student/ directory.
Now I have head.phtml file in this location:
/student/application/views/scripts/index/head.phtml
When I try to include head.phtml file in
/student/application/modules/test/views/scripts/all/index.phtml
Like this:
echo $this->partial('index/head.phtml');
It gives me following error:
Message: script 'index/head.phtml' not found in path (/var/www/student/application/modules/notification/views/scripts/)
Including files is always a difficult job for me. How to fix this. I have to include this file in many modules then what is permanent solution for this that I should not guess the path
Thanks
You can add several path to look for script view files. The best way is to do it in the bootstrap file for all your common files (like head, footer, metas...).
Just add a setupView method in your bootstrap where you deal with everything which is realted to your views :
protected function _initView()
{
$view = new Zend_View();
// setup your view with jquery and other stuffs...
// [ ... ]
// add the view directory to the stack of scripts paths
$view->addScriptPath(APPLICATION_PATH . '/views/scripts/');
}
<? $this->setScriptPath('/student/application/views/scriptss'); ?>
<?= $this->partial('index/head.phtml') ?>
I added following line in Controller's init() function.
public function init() {
$this->view->addScriptPath( APPLICATION_PATH . '/views/scripts/' );
}
Add head.phtml file in view like this:
echo $this->partial('index/head.phtml');
File is successfully added.
Related
I'm just trying out Fat-Free Framework and I come to some trouble.
I try to use the autoloading to load one of my routes, like this :
<?php
$f3 = require 'vendor/bcosca/fatfree-core/base.php';
$f3->set('DEBUG', 3);
$f3->set('AUTOLOAD', 'app/');
$f3->config('app/routes.ini');
$f3->run();
I have a app/ dir, and a routes.ini file in it, like this :
[routes]
GET / = Test->show
Then, I have a Test.php file in app/, with this in it :
<?php
class Test {
function show($f3) {
echo 'ok !';
}
}
After running this I get a big fancy error saying the following :
Method Not Allowed
HTTP 405 (GET /)
Any ideas ? (It isn't my PHP Version, if you'd ask yourself)
Your initial code is fine. It's just that your Test class conflicts with the framework's own Test class.
Rename it to anything else and it should work.
Well, I tried some things. This actually works :
The index.php file is the same as before.
The app/ dir now has a controllers/ dir in it.
There is a file named index.php inside the controllers dir, with this in it :
<?php
namespace Controllers;
class Index {
public function get() {
echo 'Yey !';
}
}
The routes file now looks like this :
[routes]
GET / = Controllers\Index->get
And that's it ! It works.
i have a structure like so:
modules -> Controllers -> Amodule -> IndexController
there i have an action which is called:
public function getTemplateAction(){
... //1) load html file
//2) return html
}
this function is called from javascript with get via the url: baseUrl+'/Amodule/index/get-template/viewid/3.
I would like based on the viewid to return an html template, where should i put the .html file and how can i load it in Zend? i tried
this->partial('thetemplate.html');
and putting the html file in Amodule folder but didnt work.
Thanks in advance!
Added another folder at the same level as controller called it views/scripts and in there i created the partial. Then from the view i load the partial as:
<?php echo $this->partial('_partialName.phtml', array(
var1 => value1,
var2 => value2
/*... vars to pass in the partial*/
));?>
A simple solution is to have your html files in a dedicated folder at the root of your project and output the file with readfile :
public function getTemplateAction() {
readfile(APPLICATION_PATH . '/../templates/thetemplate.html');
exit;
}
In my application, to move a file to a specific directory i need to know public folder path in controller action. I read different this type solution but not getting easy one. I know that in view we can get easily public folder path using $this->basePath(); view helper. I exactly want this in controller action. Anybody can guide me how can i achieve that. Thanks in advance.
index.php sets the current working dir to you application root (the folder containing composer.json, init_autoloader.php, etc.)
As long as you haven't called chdir elsewhere in your application you can call getcwd() and it'll always return the path to your app root.
Since the public folder is relative to that, you can get the path using ...
$publicDir = getcwd() . '/public';
In your public folder edit your file named index.php
add only two lines
define('BASE_PATH', realpath(dirname(__DIR__)));
define('PUBLIC_PATH', BASE_PATH.'/public');
you can use in your code like
print_r(BASE_PATH);
print_r(PUBLIC_PATH);
If you want to include a file from public folder (independence with location of index.php file):
include_one ("./public/your-file.php");
You should try this if you want the public folder:
$publicPath = $_SERVER['DOCUMENT_ROOT'];
Or try this if you want the basepath:
$basePath = dirname($_SERVER['DOCUMENT_ROOT']);
You could use view helpers from within a controller in ZF2 as it shown here and here. You may try this for your case :
$renderer = $this->serviceLocator->get('Zend\View\Renderer\RendererInterface');
$url = $renderer->basePath('the_ressource_you_want_to_get_from_public_folder');
Is it possibile to put layout files in the modules/templates (or something else) directory instead of the general app/templates?
I've tried to put the path in view.yml but it doesn't work.
Just found a workaround so I can keep layout.php inside the module/templates folder:
public function preExecute()
{
$template = $this->getContext()->getConfiguration()->getTemplateDir('MODULE', 'layout.php');
$this->setLayout($template . '/layout');
}
my problem is fairly when I call a view helper from view script it can't be called
although I added properly all information path to the config file via this line:
resources.view.helperPath.ZF_View_Helper_="ZF/View/Helper/"
also I registered the helper in bootstrap file
function _initViewHelpers(){
$view = new Zend_View();
$view->addHelperPath('ZF/View/Helper','ZF_View_Helper');
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
}
but in vain it still printing out this error message:
Application error
Exception information:
Message: Plugin by name 'OutputHelper' was not found in the registry; used paths:
Zend_View_Helper_: Zend/View/Helper/
it doesn't include the custom view helper path as expected ;
the path of the view helper is: library/ZF/View/Helper/OutputHelper.php
can you do this:
in view script
$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
var_dump($this === $view);
var_dump($view->getHelperPaths());
exit;
I think your view instance are replaced at some point.
May be module's bootstrap have view resource?
Or it can be other obvious mistake. So obvious so you'll never think of it
btw remove that _initViewHelpers method. Zend_Application_Resource_View work just fine for that.
And if you use this method, use it correctly, eg:
$this->bootstrap('view');
$view = $this->getResource('view');
//whatever