Using PHP/HTML Files in ZendFramework

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
smartcoderin
Forum Newbie
Posts: 2
Joined: Mon Mar 31, 2008 10:39 pm

Using PHP/HTML Files in ZendFramework

Post by smartcoderin »

Hello,
I have started learning Zend Framework. I think the default template/view files are of phtml extension. Tell me how can i use php/html files instead of the phtml files for view.
When i used render('index.html') ,, it shows view error..... Is anyother settings exists?

Pls reply ...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using PHP/HTML Files in ZendFramework

Post by Christopher »

.phtml files can contain HTML, PHP or both. They are only included, never accessed by the web server directly.
(#10850)
smartcoderin
Forum Newbie
Posts: 2
Joined: Mon Mar 31, 2008 10:39 pm

Re: Using PHP/HTML Files in ZendFramework

Post by smartcoderin »

The question is how can i render a file with html extension using view's render method,,

I know phtml contain html and php tags,,, But designing is tedious with phtml files..
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using PHP/HTML Files in ZendFramework

Post by Christopher »

You can set the default extension, see the manual.
(#10850)
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Using PHP/HTML Files in ZendFramework

Post by anto91 »

There are two simple ways of doing this

Type 1:

Code: Select all

 
class youClassController extends Zend_Controller_Action {
    // add this function to each controller
    public function init() {
        $this->viewSuffix = 'php';
    }
}
 
I personaly hate this way of doing it

Type 2: the way i do it

in your bootstrap add

Code: Select all

 
Zend_Loader::loadClass('ApplicationController', '../application/controllers');
 
Create a class in controllers called ApplicationController
Example contents:

Code: Select all

 
<?php
class ApplicationController extends Zend_Controller_Action {
    
    protected $db;
    
    public function init() {
        $this->db = Zend_Registry::get('db');
        
        // Default variables
        $this->view->title = $this->_request->getControllerName();
        $this->view->baseUrl = $this->_request->getBaseUrl();
        
        // This adds a user object to view if user not logged in user class uses default values
        $this->view->user = new User(Zend_Auth::getInstance()->getIdentity());
        
    }
    
    // Redirect all unknown actions to default to avoid stupid errors
    public function __call($method, $arguments) {
        $this->_redirect('/error');
    }
 
}
?>
 
Then in the init function you add

Code: Select all

 
$this->viewSuffix = 'php';
 
then replace the extended class in your controller

Example

Code: Select all

 
class fooController extends ApplicationController {
}
 
staar2
Forum Commoner
Posts: 83
Joined: Fri Apr 06, 2007 2:57 am

Re: Using PHP/HTML Files in ZendFramework

Post by staar2 »

This is really good example i am going to use your second type example, it is good to have main controller. 8O But one simple question what does the $this->viewSufix?
anto91
Forum Commoner
Posts: 58
Joined: Mon Mar 10, 2008 10:59 am
Location: Sweden

Re: Using PHP/HTML Files in ZendFramework

Post by anto91 »

If you would have read the thread starters problem you would know.
What it does is that it change the extension of the view that includes, so it includes ".php" instead of ".phtml"
Post Reply