SetScriptPath in Zend cannot find file

I work with a Zend book map and linger for several days on an example where I change the location of the default view. The method is specified as follows:

$this->view->setScriptPath("/views/"); $this->render("news"); 

I place the news.phtml file in the views directory (and not the default views/scripts/artist ), but all I got is a message saying that the page was not found. I tried many methods from the Internet, for example typing

 $this->view->setScriptPath("/application/views/"); 

or

 $this->view->setScriptPath(APPLICATION_PATH."/views/"); 

but they all do not work.

Can someone please enlighten me?


To improve clarity, I suspect that this is due to my tuning of the machine. I am working on Mac 10.7 and I have activated the built-in PHP and Apache. Since Zend provides its own stack, will there be any collision in settings files like php.ini?
Additional changes: I apply the whole method:
 public function newsAction() { //Check if the user is logged in //Get the user id // //Get the artists $artists = array("Thievery Corporation", "The Eagles", "Elton John"); //Set the view vairables $this->_helper->viewRenderer->setRender('news'); $this->view->setScriptPath(APPLICATION_PATH.'/views/'); $this->_helper->viewRenderer->setNeverController(true)->setRender('news'); } 

I put news.phtml in the views directory. URL Type I http://localhost:10088/loudbite/artist/news . artistController is located in the controllers folder. Still not working. What's wrong?

+4
source share
3 answers

When using setScriptPath() or addScriptPath you need to specify an absolute directory path or the path to your current directory. For portability, it is better to use the absolute path.

When calling Zend_View::render() you must pass the name of the script, including its extension.

Based on your last example, try something like this:

 $view->setScriptPath(APPLICATION_PATH . '/views/'); $html = $view->render('news.phtml'); 

Just make sure the right way. My example assumes you have views in your application folder.

EDIT: If you are in a controller and want to use a different view of the script , use the Renderer View Helper :

 $this->_helper->viewRenderer->setRender('news'); 

This tells the news.phtml to look for the view script news.phtml instead of the name of your action. However, it still looks in views/scripts/controller/ for news.phtml. Therefore, you will need the following changes:

 // set view script path to the base of the views folder $this->view->setScriptPath(APPLICATION_PATH . '/views/'); $this->_helper ->viewRenderer ->setNeverController(true) // do not render into controller subdirectories ->setRender('news'); // set render script to news.phtml 

When you use the Zend Application, it does its own rendering of the view, so if you are not trying to display html for use directly, you should not use Zend_View yourself. After your controller action method completes, the Zend application will automatically display the script view and try to display it along with any layout in the browser. If you created your own Zend_View, you will need to complete the request before the action completes, and not to display any other content. There are also ways to turn off the display of a layout or view as an alternative.

+5
source

Are you using Zend_Application? Or are you just trying to use Zend_View yourself?

The code you create creates the Zend_View object and assigns it to $ view, but then you call setScriptPath in the $ this-> view, not just the Zend_View created.

If you are using Zend_Application, you can set the script path in application.ini

 // Layout resources.layout.layout = "layout" resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts" // Views resources.view.encoding = "UTF-8" resources.view.basePath = APPLICATION_PATH "/views/" 
0
source

I have found the answer. A mistake on my part, but I did not know that it was important. The first time the project is loaded into Netbeans, an index file is requested, which is index.php in the public folder. I left it empty. It must be clearly defined during the creation of a project in Netbeans. If you have already made a mistake, you can go to index.php to change the definitions.

0
source

Source: https://habr.com/ru/post/1395991/


All Articles