Zend Framework Action: URL Structure and Controller
Zend Framework Action Step By Step Tutorial - Part 2: URL is important part when we develop web application. We use them for jump between pages. As we know, every framework have rule about URL. It is a key to understanding how the framework works. Now, we will talk rule at Zend Framework.
Zend Framework breaks URL into pieces. Those pieces are laid out as follows: http://hostname/controller/action/parametes. Look at our url that we used to access hello page: http://localhost/test/zend/helloworld/web_root/. Assume http://hostname is same with http://localhost/test/zend/helloworld/web_root/. Next path is controller. Try to point your browser to http://localhost/test/zend/helloworld/web_root/index. We get same with http://localhost/test/zend/helloworld/web_root/. Why? as default, http://localhost/test/zend/helloworld/web_root/ will access index controller. Can you catch idea?
I know, you don't patient to test with other controller. How about we use other controller named "user"? Never mind. From theory above, we will access with http://localhost/test/zend/helloworld/web_root/user. Let's do it.
First, create controller name "UserController". Create a file named "UserController" within application/controllers. Enter following code:
<?php
require_once 'Zend/Controller/Action.php';
class UserController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->assign('name', 'Wiwit');
$this->view->assign('title', 'Hello');
}
}
?>
Next, create a folder named "user" within helloworld\application\views\scripts. Create a file named "index.phtml" within user. Enter following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title><? echo $this->escape($this->title); ?></title> </head> <body> <h1><?=$this->escape($this->title);?>, <?=$this->escape($this->name);?></h1> </body> </html>
Now, point your browser to http://localhost/test/zend/helloworld/web_root/user.
| Series this article: Zend Framework Action: Dynamic Content Zend Framework Action: URL Structure and Controller Zend Framework Action: URL Structure and Action Zend Framework Action: GET Parameters Zend Framework Action: Including Header and Footer blog comments powered by Disqus |

