I am new to ZF2. I am trying to use EdpDiscuss module, I've downloaded from here. I put the main file to modules, and added module to application.config.php. When I var_dump active modules
$modules = $this->getEvent()->getApplication()->getServiceManager()->get('modulemanager')->getLoadedModules();
$moduleNames = array_keys($modules);
var_dump($moduleNames);
It shows EdpDiscuss as it should, but how can I use it? There are no routes in module.config.php so it has no pages. I also tried to use it as service:
$service = $sm->getServiceLocator()->get('edpdiscuss');
but I was not succesfull either. Can you please tell me how to use this module?
Thanks
There is an example project you can use:
https://github.com/EvanDotPro/EdpForum
Basically, the service can be obtained with:
$discussService = $this->getServiceLocator()->get('edpdiscuss_discuss_service');
After that, you can use methods of the service by exploring its public interface:
https://github.com/EvanDotPro/EdpDiscuss/blob/master/src/EdpDiscuss/Service/Discuss.php
Related
I am writing a PHP template system for Slim, I have it working fine, but it is necessary to install the view file Ets.php in the correct existing location:
vendor/slim/views/Slim/Views/Ets.php
Whilst I can do it manually of course this defeats the object of composer. I was wondering if I can do it with https://getcomposer.org/doc/articles/custom-installers.md but I am having trouble following the guide as it and others only really talk about installing outside of the vendor directory.
Why do you want the views to get installed in the same place?
Have a look into http://docs.slimframework.com/#Custom-Views
You just need to extend the Slim\View. An example taken from the docs
class CustomView extends \Slim\View
{
public function render($template)
{
return 'The final rendered template';
}
}
and integrate in to slim like
$app = new \Slim\Slim(array(
'view' => new CustomView()
));
NB : Don't forget to do the autoload the classes required.
The question is as follows:
How can I get the server path to the web directory in Symfony2 from inside the controller (or from anywhere else for that reason)
What I've already found (also, by searching here):
This is advised in the cookbook article on Doctrine file handling
$path = __DIR__ . '/../../../../web';
Found by searching around, only usable from inside the controller (or service with kernel injected):
$path = $this->get('kernel')->getRootDir() . '/../web';
So, is there absolutely no way to get at least that 'web' part of the path? What if I, for example, decided to rename it or move or something?
Everything was easy in the first symfony, when I could get like everything I needed from anywhere in the code by calling the static sfConfig::get() method..
There's actually no direct way to get path to webdir in Symfony2 as the framework is completely independent of the webdir.
You can use getRootDir() on instance of kernel class, just as you write. If you consider renaming /web dir in future, you should make it configurable. For example AsseticBundle has such an option in its DI configuration (see here and here).
To access the root directory from outside the controller you can simply inject %kernel.root_dir% as an argument in your services configuration.
service_name:
class: Namespace\Bundle\etc
arguments: ['%kernel.root_dir%']
Then you can get the web root in the class constructor:
public function __construct($rootDir)
{
$this->webRoot = realpath($rootDir . '/../web');
}
You also can get it from any ContainerAware (f.i. Controller) class from the request service:
If you are using apache as a webserver (I suppose for other
webservers the solution would be similar) and are using
virtualhosting (your urls look like this - localhost/app.php then you can use:
$container->get('request')->server->get('DOCUMENT_ROOT');
// in controller:
$this->getRequest()->server->get('DOCUMENT_ROOT');
Else (your urls look like this - localhost/path/to/Symfony/web/app.php:
$container->get('request')->getBasePath();
// in controller:
$this->getRequest()->getBasePath();
You are on Symfony, think "Dependency Injection" ^^
In all my SF project, I do in parameters.yml:
web_dir: "%kernel.root_dir%/../web"
So I can safely use this parameter within controller:
$this->getParameter('web_dir');
My solution is to add this code to the app.php
define('WEB_DIRECTORY', __DIR__);
The problem is that in command line code that uses the constant will break. You can also add the constant to app/console file and the other environment front controllers
Another solution may be add an static method at AppKernel that returns DIR.'/../web/'
So you can access everywhere
UPDATE: Since 2.8 this no longer works because assetic is no longer included by default. Although if you're using assetic this will work.
You can use the variable %assetic.write_to%.
$this->getParameter('assetic.write_to');
Since your assets depend on this variable to be dumped to your web directory, it's safe to assume and use to locate your web folder.
http://symfony.com/doc/current/reference/configuration/assetic.html
For Symfony3
In your controller try
$request->server->get('DOCUMENT_ROOT').$request->getBasePath()
$host = $request->server->get('HTTP_HOST');
$base = (!empty($request->server->get('BASE'))) ? $request->server->get('BASE') : '';
$getBaseUrl = $host.$base;
Since Symfony 3.3,
You can use %kernel.project_dir%/web/ instead of %kernel.root_dir%/../web/
1- How can I register and call my plugin in bootstrap file?
2- Is it a better way to use boostrap file intead of application.ini file for registering and calling my plugins ?
Note: Iam using custom path ('Mylib/Controller/Plugin') for storing my plugins.
Actually I want to convert following 'application.ini' entries
autoloaderNamespaces[] = "Mylib_"
resources.frontController.plugins.CheckHasAccess = "Mylib_Controller_Plugin_CheckHasAccess"
into bootstrap _initPlugin function.
Can some one guide me in this regards with some sample code.
Thanks in advance
1 - You would need first to load your plugin class (via Zend_Loader or require_once)
then create your plugin yourself:
$plugin = new MyPlugin();
then you can call any public method of your plugin you want and at the end you can register it within front controller:
Zend_Controller_Front::getInstance()->registerPlugin($plugin);
2 - if your plugins need to be somehow configured before they can be used by framework - then you can create and configure them yourself (as described above). If they don't need any special actions - then you can let them to be created by Framework automatically.
I have two modules, default and mojo.
After the initial bootstraping code which is the same for both of the modules, I want, for example, to use different layouts for each module (Or use different credentials check etc).
Where do I put this: IF(module=='mojo') do this ELSE do that
If you are using Zend_Application (in ZF1.8) then you should be able to use the module specific configuration options to provide this functionality with a as explained in the relevant section in the documentation.
This would require you to set the layout in the config so it looked something like
mojo.resources.layout.layout = "mojo"
anothermodule.resources.layout.layout = "anotherlayout"
The layout would then be set automatically by the bootstrap.
The other alternative is to use a front controller plug-in that implements the preDispatch() method to set the layout based on the module name.
hmm i havent tried this
http://www.nabble.com/Quick-Guide-How-to-use-different-Layouts-for-each-module-to23443422.html#a24002073
the way i did that now was thru a front controller plugin
something like
switch ($request->getModuleName()) {
case "":
// set layout ...
}
I've looked into the subject a couple of days ago, trying to get it to work on bootstrap config alone. The big problem is that all the bootstrap files are loaded, so it gives some weird results in which layout is used.
My conclusion was that you can have the config in place, but you need to work with FrontController plugins or ActionController helpers. If you want to use config set in the application.ini and you want to load the config trough the bootstrap, helpers is the only way to go. From the helper, you can then load the ActionController and on that execute the getInvokeArgs to load the bootstrap. A lot of hastle... :)
Anyway, I've done a small implementation as an example in a blog post: http://blog.keppens.biz/2009/06/create-modular-application-with-zend.html
Goodluck,
Jeroen
Does anyone know of a way to set the default module dynamically in Zend Framework and not run into namespace issues? For example, what I want to do is have a table of modules that are allowed to be loaded, with one of them set as the default module. For example, I may have:
admin
blog
calendar
as modules that can be loaded. If I have 'blog' as the default module, then 'admin' and 'calendar' have to have their controllers namespaced (Admin_IndexController, Calendar_IndexController) while 'blog' isn't (IndexController).
If I change 'calendar' to be the default module, ZF can no longer find the classes because of the namespacing.
How do you get around that? I'm currently using the following code:
$modules = new Modules();
$activeModules = $modules->fetchActive();
foreach($activeModules as $mod) {
$loadedModules[$mod->name] = '..application/modules/' . $mod->name . '/controllers';
if($mod->default) {
$defaultModule = $mod->name;
}
}
$frontController->setControllerDirectory($loadedModules);
$frontController->setDefaultModule($defaultModule);
If you plan on changing the default module, it is probably best to namespace ALL modules, and then specify that the default module should be prefixed:
First change the "blog" module to use namespacing:
<?php
// Used to be "class IndexController"
class Blog_IndexController extends Zend_Controller_Action {
}
Then, call setParam for prefixDefaultModule option on your instance of Zend_Controller_Front:
<?php
// Allow your default module to be prefixed
$frontController->setParam('prefixDefaultModule', true);
See bug # 1831 for an explanation.
use application.ini:
resources.frontController.prefixDefaultModule = true
resources.frontController.defaultModule = default
You can make a default module actually be the deciding part in this whole process.
More specifically - make all requests for default module go to a class that then will decide what specific module is currently default one and will re-route request to it.
At least that's the way we've implemented it ;)
Sounds like the work of a preDispatch Controller Plugin.
You can modify the request to change a module based on certain request or identity/session/known data to forward or redirect on demand.