Set Module name, Controller name, and Action name in Zend framework? - zend-framework

I recently started programming with Zend Framework.I want to change module name, controller name and action name of a module in my framework through coding, while coding in the same framework.
Its name is Application(module & controller) and I want to change it to Institute. Is this possible through coding?
I searched through Google for help, but either i couldn't find it or understand it properly. Any help would be appreciated.

This is really just a case of renaming things:
Update all namespaces from Application to Institute in all the classes in the module including the Module.php
Update the name of the controller and it's entry in config/module.config.php
Make sure you update the name of your view directory if you have one in the module, ie view/application/index etc to view/institute/index and make sure you update the entry in module.config.php to the same path
Update name of Application directory to Institute
Update the name in the array of modules in modules.config.php or if you are using an earlier version application.config.php from under the modules key.
That's all I can think of you would need to do
******** EDIT ********
So the basic idea would be as follows:
Add a console in a new module (I've used zend mvc console but you should probably use https://github.com/zfcampus/zf-console as mvc console is deprecated)
module.config.php
<?php
namespace Rename;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'console' => array(
'router' => array(
'routes' => array(
'rename-module' => array(
'options' => array(
'route' => 'module rename <moduleNameOld> <moduleNameNew>',
'defaults' => array(
'controller' => Controller\IndexController::class,
'action' => 'rename'
)
)
)
)
)
),
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
];
IndexController.php
<?php
namespace Rename\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Console\Request as ConsoleRequest;
use Zend\Mvc\Console\Controller\AbstractConsoleController;
class IndexController extends AbstractConsoleController
{
public function renameAction()
{
$request = $this->getRequest();
// Make sure that we are running in a console and the user has not tricked our
// application into running this action from a public web server.
if (!$request instanceof ConsoleRequest) {
throw new \RuntimeException('You can only use this action from a console!');
}
$moduleNameOld = $request->getParam('moduleNameOld');
$moduleNameNew = $request->getParam('moduleNameNew');
$module = file_get_contents(getcwd() . "/module/$moduleNameOld/src/Module.php", 'r+');
$updatedModule = str_replace($moduleNameOld, $moduleNameNew, $module);
file_put_contents(getcwd() . "/module/$moduleNameOld/src/Module.php", $updatedModule);
rename("module/$moduleNameOld", "module/$moduleNameNew");
}
}
This can be run like
php public/index.php module rename Application Institute
I've only done renaming the module here and renaming the namespace in the Module.php
to finish this off you would need to recursively find all .php files in the Application directory and loop over applying the string replace (which should be improved really). Then you could update the view and application level config too with similar logic and using the steps above.
The code I've written is pretty bad and probably insecure but might help you along the way

Related

For Nukeface: Zend Framework 2 + Doctrine2 - Blog tutorialZend\Mvc\Controller\ControllerManager::createFromInvokable: failed retrieving

Following your blog project. Copy/paste code but ran into the problem described below. I would appreciate your assistance with the code that I have pushed to the Git repository Farsideman/Zend-Framework-2-Doctrine2---Blog-tutorial. I can't fathom it even with reading the posted answers to similar questions.
Zend\Mvc\Controller\ControllerManager::createFromInvokable: failed retrieving "blogcontrollerpost(alias: Blog\Controller\Post)" via invokable class "Blog\Controller\PostController"; class does not exist
Thanks
Farsideman
Wow, almost a year, did not realize a question had been posted. Sorry about that.
Hope you figured it out by now, just in case: the problem means that it cannot find the indicated Controller.
This most likely (and most of the times) occurs when an error is made in the configuration of a module or if the files are not in the exact right folders with exactly right capital letters.
So, make sure the structure to the Controller is as follows, starting from the root of the project:
/module/Blog/src/Controller/PostController.php
Next up, check the module.config.php file and make sure you have this, exactly as below:
'controllers' => [
'invokables' => [
'Blog\\Controller\\Post' => 'Blog\\Controller\\PostController',
],
],
Here the Blog\\Controller\\Post is an alias (alternate name) for Blog\\Controller\\PostController.
That it tries to use the alias (without Controller) is setup in the routes.config.php where it is configured that it should use a class with that aliased name and a certain function, e.g.:
//routeName: blog
//route: /blog
'blog' => [
'type' => 'Literal',
'may_terminate' => true,
'options' => [
'route' => '/blog',
'defaults' => [
'module' => 'Blog',
'controller' => 'Blog\\Controller\\Post',
'action' => 'index',
],
],
],
All of the above is present in the "Configure module" chapter of that tutorial.
Note though: the tutorial was posted about halfway 2016, here 'n' there it's a bit outdated by now.
Also, as a side, in the AbstractActionController.php class, add the following:
/**
* {#inheritdoc}
*/
public function getServiceLocator()
{
return $this->getServiceLocator();
}
This will overwrite it's parent function which includes the giant warning that this function is deprecated and its functionality removed in Zend Framework 3.

Zend 2 Plugin Manager cant find class

Im new to Zend 2 and are trying to migrate my Zend 1 project to Zend 2. I had an Acl plugin in my Zend 1 project that I shared with several apps using symlink. I thought now that I migrate to Zend 2 I'd create my own package in the Vendor folder. I downloaded the Skeleton project and tried to add my plugin as this:
in the vendor folder I create myname\commons\Acl and added a my Module.php
in myname\commons\Acl i created src\WebAcl\Controller\Plugin and a added WebAclPlugin.php with the namespace WebAcl\Controller\Plugin
In my myname\commons\Acl I created ./config and added module.config.php with the content
return array(
// added for Acl ###################################
' controller_plugins' => array(
'invokables' => array(
'WebAclPlugin' => 'WebAcl\Controller\Plugin\WebAclPlugin',
)
),
// end: added for Acl ###################################
);
When I run this I get:
Fatal error: Class 'WebAcl\Controller\Plugin\WebAclPlugin' not found in AbstractPluginManager.php on line 170
What am I doing wrong?
Edit: If I in my module specify the classmap it works
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
)
But if I use "autoload" it doesnt work
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
Edit 2: This solved the problem:
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' *.str_replace("\\", "/", __NAMESPACE__),*
),
),
Still Im trying to figure out what composer.phar actually does? See additional question:
Additional question: I read that I should add my namespace in composer.json and run composer.phar update, which adds it to auto_namespace. I did this, but do I need to when I specify it in my module? Sorry if my questions are stupied.
The plugin manager will try to load the class using new which will make the registered autoloaders try to load the class. If there isn't an autoloader that can load this class, then you'll get the fatal error.
You don't say if myname\commons\Acl is a ZF2 module or a composer loaded package.
If it's a composer package, then you need to add:
"autoload": {
"psr-4": {
"WebAcl\\": "myname/commons/Acl/src/WebAcl"
}
}
to composer.json and then run composer.phar dumpautoload.
If you want myname/commons/Acl to be a module, then you need to add a Module.php to myname/commons/Acl that looks like this:
<?php
namespace WebAcl;
class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
You now need to tell your application's ModuleManager to load this module in application.config.php:
Add 'WebAcl' to the list of 'modules'
Add the path to the module in the 'module_paths' key:
'module_paths' => array(
'WebAcl' => "./vendor/myname/commons/Acl",
'./module',
'./vendor',
),
You have to do this as there isn't a direct mapping from the namespace name (WebApi) to the path name on disk like there usually is in the ./modules directory.
The ModuleManager should now find your module and the autoloader should be able to autoload any file in the WebAcl namespace within vendor/myname/commons/Acl/src/WebApi.
Of course, the composer route is easier if you don't need any other features of a ZF2 Module.

Zend Framework - Best Way To Autoload From /Application/Api Folder

I'm trying to autoload Classes from within a folder contained in the application itself.
E.G.
/Application
|->Models
|->Custom
|->Object.php
Is this the best way to do it (from bootstrap.php)?
public function _initAutoLoad()
{
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'custom' => array(
'path' => 'custom/',
'namespace' => 'Custom',
))
));
}
Meaning from within any controller, I can call:
$object = new Custom_Object();
If you're not intending to prefix the class names with the application namespace (default Application), I'd simply put this stuff in library, eg
library/
Custom/
Object.php -> class Custom_Object
then add your Custom namespace to the autoloader in configuration (application.ini)
autoloadernamespaces[] = "Custom_"
If your class represents some kind of service, you could use the built-in Service resource type which is automatically autoloaded
application/
services/
Object.php -> class Application_Service_Object
Looks like the solution I had is the best... I can find anyways...

How mvc works in Zend framework

Thanks for previous replies..
I am trying to print Hello_world using zend framework. I wrote php file in model folder and return string value as a "Hello_world". In controller i access the value of PHP like this
$value = new TextReturner();
$this->view->setValue = $value->hello_world(); . i dont know how to access the value from controller to the view php file. I am new to zend framework. I already go through the outline structure of zend framework, i dont know how to access through codings. If anyone have idea of how to print hello_world through MVC pls guide me.
You are trying to use class $value = new TextReturner(); but your controller doesn't see that class.
Set this in your Bootstrap file that will help:
protected function _initAutoLoad() {
// Add autoloader empty namespace
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
),
),
)
);
return $resourceLoader;
}
This will be autoload all of your model class.
in view you can access your variable like this:
<?php echo $this->setValue;?>

Zend framework admin module and other modules

I want to build a CMS based on Zend Framework for my needs which has admin module with authentication and other modules (pages, users, news) which can be used as plugin modules based on the appliaction needs.
I want every module to have specific frontend and backend code, so that it could be accessed like e.g. http://localhost/mycms/pages/view/5 to view a certain page from the pages module by calling Pages controller, view action. The backend for every plugin needs to be tied to the admin and require authentication, it could be accessed like http://localhost/mycms/admin/pages/add.
The problem is that the solution I found involves removing default routes and writing custom routing for every controller action inside the plugin modules like:
$router->removeDefaultRoutes();
$route = new Zend_Controller_Router_Route_Static(
'admin/pages/add',
array(
'module' => 'pages',
'controller' => 'Pages',
'action' => 'add'
)
);
$router->addRoute('pages_pages_add', $route);
$route = new Zend_Controller_Router_Route_Regex(
'pages/view/(\d+)',
array(
'module' => 'pages',
'controller' => 'Pages',
'action' => 'view'
),
array(
'1' => 'page_id'
)
);
$router->addRoute('pages_pages_view', $route);
Do you have any ideas how I can avoid this custom routing?
Have a look at Front Controller Plugins, they may give you more flexibility...
http://framework.zend.com/manual/en/zend.controller.plugins.html
class Controller_Plugin_Foo extends Zend_Controller_Plugin_Abstract
{
public function preDispatch( Zend_Controller_Request_Abstract $request )
{
$frontController = Zend_Controller_Front::getInstance();
...
}
}