HeadBase view helper
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 9596 97 98 | <? class Zend_View_Helper_HeadBase { protected $_request; protected $_scheme; protected $_host; protected $_baseUrl; public function headBase() { return $this; } public function getRequest() { if($this->_request === null) { if(!class_exists('Zend_Controller_Front', false)) { throw new Exception('Could not get request object: frontController does not exist'); } $this->_request = Zend_Controller_Front::getInstance()->getRequest(); } return $this->_request; } public function setScheme($scheme) { $this->_scheme = (string) $scheme; return $this; } public function getScheme() { if($this->_scheme === null) { $request = $this->getRequest(); $this->_scheme = ($request === null) ? 'http' : $request->getScheme(); } return $this->_scheme; } public function setHost($host) { $this->_host = (string) $host; return $this; } public function getHost() { if($this->_host === null) { try { $this->_host = $this->getRequest()->getHttpHost(); } catch(Exception $e) { throw new Exception('Could not determine host: '.$e->getMessage()); } } return $this->_host; } public function setBaseUrl($baseUrl) { $this->_baseUrl = (string) $baseUrl; return $this; } public function getBaseUrl() { if($this->_baseUrl === null) { try { $this->_baseUrl = $this->getRequest()->getBaseUrl(); } catch(Exception $e) { throw new Exception('Could not determine baseUrl: '.$e->getMessage()); } } return $this->_baseUrl; } public function getUrl() { return $this->getScheme().'://'.$this->getHost().$this->getBaseUrl().'/'; } public function toString() { return '<base href="'.$this->getUrl().'"></base>'; } public function __toString() { return $this->toString(); } } ?> |
Comments
This view helper seems like a novel idea, but I think the implementation falls short a bit.
For one, your view helper is making the assumption that $_SERVER['HTTP_HOST'] will definitely be set. To make it worse, you're not actually providing any checks and throwing an exception if it doesn't exist, so in the event that it is not set, your application will simply have unexpected behavior which is never a good thing.
Similarly, your getBaseUrl function requires the existence of a Zend_Controller_Front object. Since the MVC structure that Zend provides is by no means a requirement for the use of the framework, this seems like a severe drawback to this helper.
Also, your view helper will always display the base tag even if you do not want it to. The nice thing about helpers like HeadMeta is that they don't display anything if you don't set anything.
Overall, I think your view helper is a little too geared for one particular scenario, but with a little work -- a mutator here, a conditional there -- it could turn out to be very helpful for many people.
I definitely see this as an improvement.
The biggest improvement you could make now is to completely uncouple the view helper from the front controller. The nice thing about the Zend Framework is that you don't have to use any particular design pattern if you don't want to, so there are plenty of people out there that develop applications without the MVC components. You can make your view helper useful to those developers as well if you provide them with the means to, at the very least, set their own request object.
You must login before commenting on a snippet. If you do not have an account, please register.
Snippet description
A helper allowing one to write urls from the baseUrl without the need to repeat yourself.
In your layout:
<head>
<?=$this->headBase()?>
</head>
Snippet details
- Created:
-
JurJean
- Edited:
-
JurJean
- Revision Id:
- 8
- Edit Message:
- Initial Release
- Tags:
- view helper headbase
- Comments:
- 4
- Views:
- 1119
- Points:
- 0 (0 votes)
2 years ago
I like it, but looks like you're missing something. The getUrl method calls a getBase() method that isn't there?