creating dynamic navigation in zf3 - php

I am using zend framework3 in my project. I am able to create static navigation by following the docs link
Now I have to fetch the menu data from database then create the navigation.
For this i am using i have provide the configuration into the module.config.php which is config file of the album module.
<?php
namespace Album;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\Navigation\Service\DefaultNavigationFactory;
use Album\Navigation\AlbumNavigationFactory;
return [
'controllers' => [
'factories' => [
Controller\AlbumController::class => Factory\AlbumControllerFactory::class,
Controller\IndexController::class => InvokableFactory::class,
],
],
// Add this section:
'service_manager' => [
'factories' => [
'navigation' => Navigation\AlbumNavigationFactory::class,
Model\AlbumTable::class => Factory\AlbumTableFactory::class,
],
],
// The following section is new and should be added to your file:
'router' => [
'routes' => [
'album' => [
'type' => Segment::class,
'options' => [
'route' => '/album[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\AlbumController::class,
'action' => 'index',
],
],
],
'index' => [
'type' => Segment::class,
'options' => [
'route' => '/index[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
'album' => __DIR__ . '/../view',
],
],
];
In zend framework2 we simple pass a navigation key with factory class as
return array(
'factories' => array(
'Navigation' => 'Album\Navigation\AlbumNavigationFactory'
),
);
In zend framework3 i am doing the same thing as below
'service_manager' => [
'factories' => [
'navigation' => Navigation\AlbumNavigationFactory::class,
Model\AlbumTable::class => Factory\AlbumTableFactory::class,
],
],
I am using Navigation\AlbumNavigationFactory::class to call factory for fetching the data.
but i am not be able to get the navigation. Any help would be appreciated.

here is part of my code. I think will help. Work perfect.
In Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'ItemsFromDatabase::class => Navigation\BlogNavigationFactory::class,
)
);
}
public function getViewHelperConfig() {
return[
'factories' => [
'AddItemsInNavigation' => function($helpers) {
$navigation = $helpers->get('Application')->getServiceManager()->get('Zend\Navigation\Default')->findOneByLabel('Blog');
$newItems = $helpers->get(ItemsFromDatabase::class);
return new View\Helper\AddItemsInNavigation($navigation, $newItems);
},
],
Blog\View\Helper\AddItemsInNavigation.php
<?php
namespace Blog\View\Helper;
use Zend\View\Helper\AbstractHelper;
class AddItemsInNavigation extends AbstractHelper {
protected $navigation;
protected $newItems;
public function __construct($navigation, $newItems) {
$this->navigation = $navigation;
$this->newItems = $newItems;
}
public function addItems() {
return $this->navigation->addPages($this->newItems);
}
}
In layout
<?php
$this->AddItemsInNavigation()->addItems(); //plugin
$nawDef = $this->navigation('Zend\Navigation\Default')->menu();
echo $nawDef->setMinDepth(0)->setMaxDepth(4)->setUlClass('nav navbar-nav');
?>
W Blog\Navigation\BlogNavigationFactory.php
<?php
namespace Blog\Navigation;
use Interop\Container\ContainerInterface;
use Zend\Navigation\Navigation;
use Zend\Navigation\Service\DefaultNavigationFactory;
class BlogNavigationFactory extends DefaultNavigationFactory {
protected $pages;
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
return new Navigation($this->getPages($container));
}
protected function getPages(ContainerInterface $container) {
$navigation = array();
if (null === $this->pages) {
$navigation[] = array ( //for exemple
'label' => 'Jaapsblog.nl',
'uri' => 'http://www.jaapsblog.nl'
);
$mvcEvent = $container->get('Application')
->getMvcEvent();
$routeMatch = $mvcEvent->getRouteMatch();
$router = $mvcEvent->getRouter();
$pages = $this->getPagesFromConfig($navigation);
$this->pages = $this->injectComponents(
$pages, $routeMatch, $router
);
}
return $this->pages;
}
}

cd.
In module.config.php
'navigation' => array(
'default' => array(
'blog' => array(
'label' => 'Blog',
'route' => 'blog-front',
'controller' => 'blog',
'action' => 'index',
)
)
)

I don't know if it's that are you looking for, but i recommend to take a look on this page:
https://github.com/fabiopaiva/zf2-navigation-bootstrap3

Related

"Unable to resolve service "Album\Controller\AlbumController" to a factory; are you certain you provided it during configuration?" in Zend Framework 2

I am getting an error in Zend Framework 2 tutorial project as,
Unable to resolve service "Album\Controller\AlbumController" to a factory; are you certain you provided it during configuration?
My Module.php as
namespace Album;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '/../config/module.config.php';
}
public function getServiceConfig()
{
return [
'factories' => [
Model\AlbumTable::class => function($container) {
$tableGateway = $container->get(Model\AlbumTableGateway::class);
return new Model\AlbumTable($tableGateway);
},
Model\AlbumTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
],
];
}
public function getControllerConfig()
{
return [
'factories' => [
Controller\AlbumController::class => function($container) {
return new Controller\AlbumController(
$container->get(Model\AlbumTable::class)
);
},
],
];
}
}
And my module.config.php is;
namespace Album;
use Zend\Router\Http\Segment;
//use Zend\ServiceManager\Factory\InvokableFactory;
return [
'controllers' => [
'factories' => [
Controller\AlbumController::class => InvokableFactory::class,
],
],
'router' => [
'routes' => [
'album' => [
'type' => Segment::class,
'options' => [
'route' => '/album[/:action[/:id]]',
'constraints' => [
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
'controller' => Controller\AlbumController::class,
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
'album' => __DIR__ . '/../view',
],
],
];
I am very new in Zend Framework. Just can not figure out what I am doing wrong. Please mention if some more code is necessary.
You have defined two factories for "Controller\AlbumController::class"...
One in module.config.php -
return [
'controllers' => [
'factories' => [
Controller\AlbumController::class => InvokableFactory::class,
],
],
],
and the second one in Module.php-
public function getControllerConfig()
{
return [
'factories' => [
Controller\AlbumController::class => function($container) {
return new Controller\AlbumController(
$container->get(Model\AlbumTable::class)
);
},
],
];
}
They are both conflicting with each other. Invokable factories are for classes that doesn't have have any dependencies. In this case it looks like "AlbumController" has a dependency of "AlbumTable", so the second option looks like the one you want.
So in short, remove the 'controllers' key value from the array in module.config.php.
Hope this helps!
I had the same problem, and a few other related problems - until I finally realized the real source of the issues: I was following the tutorial for Zend 2, while what I had installed is Zend 3!
If you have Zend 3, by all means use the tutorial for Zend 3, here:
https://docs.zendframework.com/tutorials/getting-started/overview/

Customer service and service factory not registering in ZF3

For some reason, I don't think that my 'service_manager' config is being read properly. This is mostly pretty much a brand-new skeleton checkout. Maybe a days work.
I did another one recently and tried comparing. I can't figure out where I went wrong.
Under the anonymous function pointed to by Controller\DbBuilderController::class => function(ContainerInterface $container, $requestedName) { the line $userService = $container->get(\Application\Service\UserService::class); causes an error: Unable to resolve service "Application\Service\UserService" to a factory; are you certain you provided it during configuration?
I have tried changing \Application\Service\UserService::class to short, silly, literal strings so I'm confident that the service is not being registered.
I'm not sure why that would happen. Any takers?
<?php
namespace Application;
use Zend\Mvc\Application;
use Zend\Mvc\Controller\LazyControllerAbstractFactory;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
use Interop\Container\ContainerInterface;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
return [
'service_manager' => [
'factories' => [
\Application\Service\UserService::class => \Application\Service\Factory\UserServiceFactory::class
],
],
'doctrine' => [
'driver' => [
__NAMESPACE__ . '_driver' => [
'class' => AnnotationDriver::class,
'cache' => 'array',
'paths' => [__DIR__ . '/../src/Entity']
],
'orm_default' => [
'drivers' => [
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
]
]
]
],
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route' => '/',
'defaults' => [
'controller' => Controller\IndexController::class,
'action' => 'index',
],
],
],
'createUser' => [
'type' => Segment::class,
'options' => [
'route' => '/createuser/:username/:password',
'defaults' => [
'controller' => Controller\DbBuilderController::class,
'action' => 'createUser',
'username' => '',
'password' => ''
],
],
],
'importTrades' => [
'type' => Literal::class,
'options' => [
'route' => '/importTrades',
'defaults' => [
'controller' => Controller\DbBuilderController::class,
'action' => 'importTrades',
],
],
],
'createExchanges' => [
'type' => Literal::class,
'options' => [
'route' => '/createExchanges',
'defaults' => [
'controller' => Controller\DbBuilderController::class,
'action' => 'createExchanges',
],
],
],
],
],
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
Controller\DbBuilderController::class => function(ContainerInterface $container, $requestedName) {
$entityManager = $container->get('doctrine.entitymanager.orm_default');
$userService = $container->get(\Application\Service\UserService::class);
return new Controller\DbBuilderController($entityManager, $userService);
},
],
],
'view_manager' => [
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => [
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
],
'template_path_stack' => [
__DIR__ . '/../view',
],
],
'strategies' => array(
'ViewJsonStrategy',
),
];
The factory:
<?php
namespace Application\Service\Factory;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
class UserServiceFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$entityManager = $container->get('doctrine.entitymanager.orm_default');
$user = new \Application\Service\UserService($entityManager);
return $user;
}
}
The Service:
<?php
namespace Application\Service;
class UserService
{
protected $entityManager;
function __construct(\Doctrine\ORM\EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function createUser($username, $password)
{
$user = new \Application\Entity\User();
$user->setUserKey($password);
$user->setUserName($username);
$user->setIsAdmin(true);
$this->entityManager->persist($user);
$this->entityManager->flush();
return $user;
}
}
Looking at the ZF3 service manager suggests it can't load the factory class, i.e. class_exists('Application\Service\Factory\UserServiceFactory') is failing.
Make sure the autoloader is registered for the Application namespace, either using the getAutoloaderConfig function in Module.php or by adding into the composer.json:
"autoload": {
"psr-4": {
"Application\\": "module/Application/src/"
}
},
(obviously with the directory changed as necessary) then run composer update.
Maybe also check the directory structure containing UserServiceFactory for typos if you're using the Zend\Loader\StandardAutoloader.

Error on login connection with ZF2

Currently I am creating an authentication module.
I work with Zend Framework 2.5.1, coupled Doctrine 2.
When I try to connect, once posted my form, I have the following error: Missing parameter "id"
Here is my login action (in AuthController):
public function loginAction()
{
/** #var ServiceLocator $serviceLocator */
$serviceLocator = $this->getServiceLocator();
/** #var Authenticate $authentication */
$authentication = $serviceLocator->get('service.user.auth');
if ($authentication->hasIdentity()) {
$this->redirect()->toRoute('home/user',
[
'controller' => 'user',
'action' => 'profile',
], [], true);
}
if (($prg = $this->postRedirectGet()) instanceof Response) {
return $prg;
}
/** #var Login $form */
$form = $authentication->getForm();
if ($prg !== false) {
$form->setData($prg);
if ($authentication->authenticate()) {
$this->redirect()->toRoute('home/user',
[
'controller' => 'user',
'action' => 'profile',
], [], true);
}
if ($form->isValid()) {
$messages = $authentication->getMessages();
}
}
return compact('form', 'messages');
}
And the routing that matches my authentication:
return
[
'router' =>
[
'routes' =>
[
'home' =>
[
'child_routes' =>
[
'user' =>
[
'type' => 'Segment',
'options' =>
[
'route' => 'user[/:action][/:id]',
'defaults' =>
[
'controller' => 'user/user',
'action' => 'index',
],
'constraints' =>
[
'action' => 'index|register|update|profile',
'id' => '[0-9]+'
],
],
'may_terminate' => true,
'child_routes' =>
[
'auth-login' =>
[
'type' => 'Literal',
'options' =>
[
'route' => '/login',
'defaults' =>
[
'controller' => 'user/auth',
'action' => 'login'
],
'constraints' =>
[
'action' => 'login|logout',
],
],
],
],
],
],
],
],
],
];
A little help would be welcome, I'm not sure why this error ..
Big thanks!
In the config, try 'id' => '[0-9]*' with * instead of +, since the + means that it should have at least one digit.
Also, did you have a look on https://github.com/ZF-Commons/ZfcUser/ which provides such functionality for you to use. and you can use it with Doctrine, in addition to many RBAC and Access List authorizations modules...
I try everything you said, this is the same error. Debug the routing is not realy easy in ZF2.
For information, I have 2 modules: Application, and User.
So it's the routing for Application module:
<?php
return
[
'router' =>
[
'routes' =>
[
'home' =>
[
'type' => 'Literal',
'options' =>
[
'route' => '/',
'defaults' =>
[
'controller' => 'application.index',
'action' => 'index',
],
],
'may_terminate' => true,
],
],
]
];

ZF2 - How to add a new Form in an existing Controller?

I have a login Form LoginForm.php with its Filter LoginFilter.php, that has a View /login/index.phtml, a Controller LoginController.php, two Factory LoginControllerFactory.php & LoginFormFactory.php and it is called in the config.module.php and works perfect. The Form is correctly displayed.
I have a ViewController.php that has a method idAction that shows a post by its id passed by parameter from the homepage in a View called /view/id.phtml. I want to display this Form I created within this View and I don't know how. First, I created the Form exactly as I created the login Form, but I realized that I already configured my id child-route, inside of view route with a Factory in module.config.php.
Then, I tried to set the form in the idAction method, exactly as I did in indexAction in LoginController.php Controller, but I'm receiving the following error: An exception was raised while creating "Rxe\Factory\ViewController"; no instance returned.
I will now show you what I did to try to display this new Form.
First, the Form itself:
class CommentForm extends Form
{
public function buildForm()
{
$this->setAttribute('method', 'POST');
$this->setAttribute('id', 'add-comment-form');
$this->add(array(
'name' => 'comment',
'type' => 'textarea',
'options' => array(
'label' => 'Category'
),
'attributes' => array(
'class' => 'form-control'
)
));
$this->add(array(
'name' => 'submit',
'type' => 'submit',
'attributes' => array(
'class' => 'btn btn-success',
'value' => 'Comment'
)
));
}
}
Form's CommentFormFactory.php calling its Filter and building the Form:
class CommentFormFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$form = new CommentForm();
$form->setInputFilter($serviceLocator->get('Rxe\Factory\CommentFilter'));
$form->buildForm();
return $form;
}
}
The ViewControllerFactory.php calling the CommentFormFactory.php, just like in LoginControllerFactory.php:
class ViewControllerFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$serviceManager = $serviceLocator->getServiceLocator();
$viewController = new ViewController();
$viewController->setPostsTable($serviceManager->get('Rxe\Factory\PostsTable'));
$viewController->setCommentsTable($serviceManager->get('Rxe\Factory\CommentsTable'));
$viewController->setCommentForm($serviceManager->get('Rxe\Factory\CommentForm'));
return $viewController;
}
}
The ViewController.php, calling the form within its idAction's ViewModel:
class ViewController extends AbstractActionController
{
use PostsTableTrait;
use CommentsTableTrait;
private $commentForm;
function setCommentForm($commentForm)
{
$this->commentForm = $commentForm;
}
public function indexAction()
{
$category = $this->params()->fromRoute('category');
return new ViewModel(array(
'posts' => $this->postsTable->getPostsByCategory($category),
'categories' => $category
));
}
public function idAction()
{
$id = $this->params()->fromRoute('id');
$viewModel = new ViewModel(array(
'commentForm' => $this->commentForm,
'commentParams' => $this->params()->fromPost(),
'messages' => $this->flashMessenger()->getMessages(),
'posts' => $this->postsTable->getPostById($id),
'posts' => $this->commentsTable->getNumberOfCommentsByPost($id),
'comments' => $this->commentsTable->getCommentsByPost($id)
));
$viewModel->setTemplate('rxe/view/id.phtml');
if ($this->getRequest()->isPost()) {
$this->commentForm->setData($this->params()->fromPost());
if ($this->commentForm->isValid()) {
$this->flashMessenger()->addMessage('Thank you for your comment. :)');
} else {
$this->flashMessenger()->addMessage('Your comment wasn\'t sent.');
}
}
return $viewModel;
}
}
And finally my module.config.php
'controllers' => array(
'invokables' => array(
'Rxe\Controller\Index' => 'Rxe\Controller\IndexController',
'Rxe\Controller\View' => 'Rxe\Controller\ViewController',
'Rxe\Controller\Login' => 'Rxe\Controller\LoginController'
),
'factories' => array(
'Rxe\Factory\LoginController' => 'Rxe\Factory\LoginControllerFactory',
'Rxe\Factory\ViewController' => 'Rxe\Factory\ViewControllerFactory',
'Rxe\Factory\IndexController' => 'Rxe\Factory\IndexControllerFactory'
)
),
'service_manager' => array(
'factories' => array(
'Rxe\Factory\LoginForm' => 'Rxe\Factory\LoginFormFactory',
'Rxe\Factory\LoginFilter' => 'Rxe\Factory\LoginFilterFactory',
'Rxe\Factory\CommentForm' => 'Rxe\Factory\CommentFormFactory',
'Rxe\Factory\CommentFilter' => 'Rxe\Factory\CommentFilterFactory',
'Rxe\Factory\PostsTable' => 'Rxe\Factory\PostsTableFactory',
'Rxe\Factory\CategoriesTable' => 'Rxe\Factory\CategoriesTableFactory',
'Rxe\Factory\CommentsTable' => 'Rxe\Factory\CommentsTableFactory',
'Zend\Db\Adapter\AdapterService' => 'Zend\Db\Adapter\AdapterServiceFactory'
)
),
Please, let me know if you need me to show you more codes. Thank you in advance.
EDIT #1
If I remove the line that calls the Form in the ViewControllerFactory.php, I get the following error: Fatal error: Call to a member function prepare() on a non-object in /home/vol12_3/byethost4.com/b4_16354889/htdocs/module/Rxe/view/rxe/view/id.phtml on line 31
The id.phtml is:
<!-- Comment form -->
<div id="comment-form-area" class="col-xs-3">
<?php $this->commentForm->prepare() ?>
<?php echo $this->form()->openTag($this->commentForm); ?>
<div class="form-group comment-area">
<?php echo $this->formRow($this->commentForm->get('comment_content')); ?>
</div>
<div class="form-group">
<?php echo $this->formRow($this->commentForm->get('submit')); ?>
</div>
<?php echo $this->form()->closeTag(); ?>
</div>
<!-- /Comment form -->
Try removing these lines
'invokables' => array(
'Rxe\Controller\Index' => 'Rxe\Controller\IndexController',
'Rxe\Controller\View' => 'Rxe\Controller\ViewController',
'Rxe\Controller\Login' => 'Rxe\Controller\LoginController'
),
If it doesn't work, have a look at this tutorial how to create proper controller factories and pass dependencies. https://samsonasik.wordpress.com/2015/03/31/zend-framework-2-using-__invokepluginmanager-manager-in-services-factory/
An example how I build my forms:
namespace Admin\Form;
use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;
class ContentForm extends Form implements InputFilterProviderInterface
{
public function __construct()
{
parent::__construct("content");
}
public function init()
{
$this->setAttribute('method', 'post');
$this->add([
'type' => 'Zend\Form\Element\Text',
'name' => 'title',
'attributes' => [
'required' => true,
'size' => 40,
'id' => "seo-caption",
'placeholder' => 'Title',
],
'options' => [
'label' => 'Title',
],
]);
$this->add([
'type' => 'Zend\Form\Element\Text',
'name' => 'text',
'attributes' => [
'class' => 'ckeditor',
'rows' => 5,
'cols' => 80,
],
'options' => [
'label' => 'Text',
],
]);
}
public function getInputFilterSpecification()
{
return [
[
"name"=>"title",
"required" => true,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
['name' => 'NotEmpty'],
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 200,
],
],
],
],
[
"name"=>"text",
"required" => true,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
['name' => 'NotEmpty'],
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
],
],
],
],
];
}
}
Than I create a Factory
namespace Admin\Factory\Controller;
use Admin\Controller\ContentController;
use Zend\Mvc\Controller\ControllerManager;
class ContentFormFactory
{
/**
* #{inheritDoc}
*/
public function __invoke(ControllerManager $controllerManager)
{
return new ContentController(
$controllerManager->getServiceLocator()->get('FormElementManager')->get('Admin\Form\ContentForm')
);
}
}
Inside module.config.php I have this code
'controllers' => [
'factories' => [
'Admin\Controller\Content' => "Admin\Factory\Controller\ContentFormFactory",
],
'invokables' => [
...
],
],
Please, show us some more code.

Zend Framework 2 - How make views separation for frontend and backend?

I'm a beginner at zf2 and trying to do some sort of folder structuring so make them nice and manageable.
I'm trying to structure my controllers and views in such way that backend related files are in their folder and frontend will be in theirs. I managed to separate my controllers with folders and namespacing them, (ex. Blog\Controller\Frontend\Blog & Blog\Controller\Backend\Blog) and I can make call to them as well using the invokables in the config. However I cannot do that same in the views (Ex. view\blog\frontend\blog & views\blog\backend\blog) as it is looking only in views\blog\blog folder.
Can anyone help how can I fix it so that my views can have some separation of folders for each end?
My module.config.php looks like the following:
<?php namespace Blog;
return [
'controllers' => [
'invokables' => [
// 'Blog\Controller\Blog' => 'Blog\Controller\BlogController',
'Blog\Controller\Frontend\Blog' => 'Blog\Controller\Frontend\BlogController',
'Blog\Controller\Backend\Blog' => 'Blog\Controller\Backend\BlogController',
],
],
'router' => [
'routes' => [
'blog' => [
'type' => 'segment',
'options' => [
'route' => '/blog/blog[/:action][/:id]',
'constraints' => [
// 'controller' => 'Blog\Controller\Blog',
'controller' => 'Blog\Controller\Frontend\Blog',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
],
'defaults' => [
// 'controller' => 'Blog\Controller\Blog',
'controller' => 'Blog\Controller\Frontend\Blog',
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
'blog' => __DIR__ . '/../view',
],
],
// Doctrine config
'doctrine' => [
'driver' => [
__NAMESPACE__ . '_driver' => [
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => [
__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'
],
],
'orm_default' => [
'drivers' => [
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
],
],
],
],
];
First of all, you could've come with Blog\Controller\BlogFrontend and Blog\Controller\BlogBackend classes. It would've made your life a little bit easier.
If, for one reason or another, you don't want to rename them, you can set a rendering template manually:
class IndexController extends AbstractActionController
{
public function indexAction()
{
$view = new ViewModel();
$view->setTemplate ('application/frontend/blog/index.phtml');
return $view;
}
}
and in the admin controller
class IndexController extends AbstractActionController
{
public function indexAction()
{
$view = new ViewModel();
$view->setTemplate ('application/backend/blog/index.phtml');
return $view;
}
}

Categories