I went through ZF tutorial for creating Album.
The structure would be as below :-
module/
Album/
config/
module.config.php
src/
Album/
Controller/
AlbumController.php
view/
album/ ....
Module.php
I need to create one more controller called UserController.php under src/Album/Controller/
IS this possible? Is this the right way to do in Zendframework2?
You need to define the new controller in the module.config.php
'controllers' => array(
'invokables' => array(
[... some other controller defined here too ...]
'Album\Controller\User' => 'Album\Controller\UserController',
)
)
then you create your new Controller in Album/src/Album/Controller/ and name it UserController.php. The file becomes the namespace and the class name:
namespace Album\Controller;
class UserController extends \Zend\Mvc\Controller\AbstractActionController
and some indexAction method witch returns a new \Zend\View\Model\ViewModel();
then you can create your viewfile in
Album/view/Album/user/index.phtml
and yes, this is the normal procedure to add a new controller to a module.
Related
I am getting one problem in zend framework. I want to hide controller name in my zend framework url.
For Example https://example.com/index/about-us
I want to hide index controller from my url and i want url like
https://example.com/about-us
index is my default controller and about-us is my method
I have tried following code but it doesn't help
i have put this code in /application/Bootstrap.php
public function _initCustomRoute(){
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('default-override', $route);
}
please help me to solve this problem.
Using ZF2 to customise an Entity based on ZfcUser. Trying to use ScnSocialAuth and got a bit of a problem.
The problem is that I am using custom routes ('/account' instead of '/user') and when implementing ScnSocialAuth I cannot get the social code into my custom zfcuser view...?
I have \\view\zfc-user\user\register.php which overrides the zfcuser registration.
I have a customised route:
'account' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/account',
),
),
These are my zfc config modification within \my-module\config\module.config.php
'zfcuser' => array(
// telling ZfcUser to use our own class
'user_entity_class' => 'WMember\Entity\WMember',
// telling ZfcUserDoctrineORM to skip the entities it defines
'enable_default_entities' => false,
'table_name' => 'w_member',
'login_redirect_route' => 'account',
),
My global \config\application.config.php
'ScnSocialAuth',
'MyModule1',
'ZfcBase',
'ZfcUser',
'BjyAuthorize',
'GoalioMailService',
'GoalioForgotPassword',
'my-user-module',
Therefore, after all this:
I can see my own extended User registration form by navigating to
/account/register with no Social login links visible
I can see the ScnSocialAuth when navigating to /user/register
a) I cannot create the view in my module to override \vendor\scn-social-auth\user\register.phtml as was done with zfcuser
Please help with getting ScnSocialAuth to work with my custom route setup.
If this is just wrong please let me know as I'm not ZF2 expert. Happy to take 'constructive' criticism.
Saw these posts: How to (correctly) extend ScnSocialAuth\Authentication\Adapter\HybridAuth::authenticate() method?
and this as a result of the above post:
https://github.com/SocalNick/ScnSocialAuth/issues/202
NOTE: still running ZF-2.3* due to PHP 5.3,5.4
Instead of adding a custom route to your config, you need to over-ride the zfcuser route
<?php
// #file MyUserModule/config/module.config.php
return array(
// other config ...
'router' => array(
'routes' => array(
'zfcuser' => array(
'options' => array(
// this is the only change needed to route zfcuser to /account
'route' => '/account',
),
),
),
),
// more config ...
);
The ScnSocialAuth module uses the forward() plugin to render the content from zfcusers register view (and login view iirc), which means it will only ever look at the zfcuser route and completely ignore your custom route. The only way to have it use your custom route would be to replace ScnSocialAuths UserController with your own using identical code but forwarding to your custom route (much more work there, and still the potential to break anything else that expects zfcuser to be the route used)
I need to autoload old style classes in ZF2. The classes are located in vendor directory.
What is the best way to this?
ZF's StandardAutoloader class provides a fallback autoloading mechanism for non-namespaced classes. Try to set fallback_autoloader key to true in your Application module's getAutoloaderConfig() method something like below:
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src',
'Ancientlib' => '/path/to/ancient/library'
),
'fallback_autoloader' => true,
)
);
}
Also there is an article by Rob Allen about this subject.
Provides more details about this subject: Using Zend\Loader\Autoloader
I would like build a menu in Skeleton Application (ZF2). I made a separate module Navigation.
At Navigation Controller wrote a code:
namespace Navigation\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Navigation\Navigation;
class NavigationController extends AbstractActionController
{
public function indexAction()
{
$container = new \Zend\Navigation\Navigation(array(
array(
'label' => 'Album',
'controller' => 'album',
'action' => 'index',
'route' => 'album',
)
));
$navigation = new \Zend\View\Helper\Navigation\Menu();
$navigation->renderMenu($container);
return new ViewModel(array('navigation'=>$navigation));
}}
And get the mistake:
Zend\Navigation\Page\Mvc::getHref cannot execute as no Zend\Mvc\Router\RouteStackInterface instance is composed
At file: \vendor\zendframework\zendframework\library\Zend\Navigation\Page\Mvc.php:186
How resolve it?
It seems that the route 'album' is not defined within your Navigation module. Routing needs to be set up in module.config.php of your Navigation module. The route needs to contain an additional parameter :module which points to the Application module, I think.
Click the link
to download ZF2 dynamic navigation from database. There are functionality for add, edit, delete menu items.
I have an application with 3 modules and route configs like below:
admin.domain.tld/[:controller[:/action]] => Admin
rest.domain.tld/[:controller[:/id]] => Rest
domain.tld/[:controller[:/action]] => Site
and set DI alias for all controller in each modules
REST Module DI Alias:
'alias' => array(
'index' => 'Rest\Controller\IndexController',
...
),
Admin Module DI Alias:
'alias' => array(
'index' => 'Admin\Controller\IndexController',
...
),
Site Module DI Alias:
'alias' => array(
'index' => 'Site\Controller\IndexController',
...
),
As you see, some controllers has same name (eg: IndexController), but since zf2 merged config with LIFO behaviour, 'index' alias always from the last added module.
Application Config
'modules' => array('Rest','Admin', 'Site'),
when i access http://admin.domain.tld/ I expect index alias gives Admin\Controller\IndexController but since Site Module (registered last) has same alias for index it gives Site\Controller\IndexController
How to use different DI alias to match same controller name?
Before the new view layer was merged into master, it was required to have aliases for controllers to behave correctly when resolving view scripts. Now this is not required anymore, it is even not recommended anymore to use aliases for controllers. The problem with aliasing is there is one alias for one FQCN, so your problem is directly related to this.
What you need to do is to remove aliases from the DI configuration and use explicit routes instead. The "magic" route [:controller[/:action]] is a bad practice and results in more problems than it can help you. So write some explicit routes and remove the aliases.