Skip to content

Instantly share code, notes, and snippets.

@bitkorn
Last active October 31, 2016 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitkorn/90bbb11bade50f91ba6103f89a2070c5 to your computer and use it in GitHub Desktop.
Save bitkorn/90bbb11bade50f91ba6103f89a2070c5 to your computer and use it in GitHub Desktop.
In ZF2 Controller: Get the route from URL and look at database for content to show it in view.
<?php
/**
* Get the route from URL and look at database for content to show it in view.
* @return ViewModel
*/
public function routesCmsAction()
{
$routeMatch = $this->serviceLocator->get('Application')->getMvcEvent()->getRouteMatch();
$routeName = $routeMatch->getMatchedRouteName();
$router = $this->getEvent()->getRouter();
$route = substr($router->assemble([], array('name' => $routeName)), 1); // cut the slash
$contentArr = $this->getContentTable()->getContentByRoute($route);
$metaTitle = '';
$metaDescription = '';
$metaKeywords = '';
$contentString = '<div class="startcontent">';
if (!empty($contentArr)) {
$metaTitle = $contentArr['content_meta_title'];
$metaDescription = $contentArr['content_meta_description'];
$metaKeywords = $contentArr['content_meta_keywords'];
if (!empty($contentArr['content_title'])) {
$contentString .= '<h4>' . $contentArr['content_title'] . '</h4>';
}
$contentString .= $contentArr['content'];
}
$contentString .= '</div>';
return new ViewModel([
'textarea' => null,
'content' => $contentString,
'meta_title' => $metaTitle,
'meta_description' => $metaDescription,
'meta_keywords' => $metaKeywords,
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment