Get module_path from environment variable in Zend Framework 2 - php

I have a few modules and the Zend Framework library in "C:\Public\vendor" folder. The directory looks like this:
vendor
--> Module1
--> Module2
--> ZF2
----->library
------->Zend
In my PHP application, which is actually located in "C:\Public\apps\myApplication", I am able to get the Zend Framework path, by setting a ZF2_PATH environment variable in my apache's httpd-vhosts.conf file.
However, to get the Modules to load, in application.config.php, I am having to mention the physical path of the module location (which might change in a production environment), ex:
'module_paths' => array(
'./module',
'./vendor',
'C:\\Public\\vendor',
),
Is there a way to set and retrieve, module_paths using an environment variable in apache?

Why forget about normal PHP just when working with a Framework?
$modulePaths = array();
switch ($env) {
case 'local' : $modulePaths[] = ....;
// other cases ....
}
'module_paths' => $modulePaths,

I modified my code like this to read the environment variable, set in apache and it worked..
'module_paths' => array(
'./module',
'./vendor',
getenv('CUSTOM_MODULES_PATH'),
),
Thanks for your input Sam..

Related

How to access at the local configuration on Zend Framework 2

I have some configuration on local/config/xxx/config.ini and a local/config/xxx/config_base.ini.
How can I access at there configuration on Zend Framework 2. I have an instance of the ServiceProvider but I cannot access at these config. It only ready the module.config.php (ex with $config = $sm->get('Config');)
I found that I can use this for read an ini file:
$config = (new Zend\Config\ReaderIni())->fromFile(getcwd() . '/local/config/xxx/config.ini');
But then how to merge the two configurations?
Why would do you use .ini extension for your config files? As you can read in the documentation on Advanced Configuration Tricks config files should be stored as .php files. They contain arrays holding the keys and values of your configuration and they will be automatically read from the autoload folders and merged with the module configuration files.
Once configuration is aggregated from all modules, the ConfigListener will also merge application configuration globbed in specified directories (typically config/autoload/).
Here you can also find the folder pattern used to find the files it needs to merge:
// An array of paths from which to glob configuration files after
// modules are loaded. These effectively overide configuration
// provided by modules themselves. Paths may use GLOB_BRACE notation.
'config_glob_paths' => array(
'config/autoload/{{,*.}global,{,*.}local}.php',
),
The advantage is that you don't need a separate file reader and you can access your custom config from the service manager directly like you would do for other configs.
$config = $sm->get('Config');
$webhost = $config['my_param'];
Another advantage is that you can setup your production environment to cache the config files which will dramatically increase the performance of the bootstrapping procedure (reading files is a slow process).
I would suggest you rename your file extension to .php and follow the documentation for setting up your custom configuration values.
When using the skeleton application, the system configuration is by default in config/application.config.php.
/ config.php
return array(
'webhost' => 'www.ekio.rw',
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.ekio.rw',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
Then,
// Consumes the configuration array
$config = new Zend\Config\Config(include 'config.php');
// Display a configuration datum (results in 'www.ekio.rw')
echo $config->webhost;

Trying to get started with Zend Framework 1.12.7

Hi guys I am having a lot of trouble getting started with Zend Framework 1.12. Basically I've managed to get my project set up in Netbeans and I have it located on my localhost. When I go to this URL I get the default page:
http://localhost/zendtest2/application/views/scripts/index/index.phtml
But when I go to this page I get a list of my file tree in the localhost directory
http://localhost/zendtest2/
I guess what I am trying to understand is how to I get the http://localhost/zendtest2/ to point to the http://localhost/zendtest2/application/views/scripts/index/index.phtml or is that how I would even do it?
I know there is a public folder in my project with an index.php file. Is there some way that I should be reaching that page when the project initially starts?
To me this sounds more like an environment issue rather than a zend framework issue. I am not entirely sure what your dev environment is, but I am going to assume you're using apache as your HTTP server:
https://wiki.apache.org/httpd/DirectoryListings
Again, I am not entirely sure what OS you're using so you'll need to find your httpd.conf file yourself.
You then have two options, adding the redirect/route there (that may be wrong) or (more recommended) uncommenting the httpd-vhosts.conf link:
# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf
Then including the new route in there. Heres a basic example (again you'll need to add what you need):
<VirtualHost *:8888>
ServerName zf2-tutorial.localhost
DocumentRoot "/Users/stevenc/****DIR_STUFF****/skeleton-application/public"
</VirtualHost>
Anything beyond that can be set as your home route in the module.config.php of the Application module within your zend project:
'router' => array(
'routes' => array(
'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
'controller' => 'BookList\Controller\Book',
'action' => 'index',
),
),
),
Also, if you're not using ZF 1.12 for any particular reason, ZF2.3* is the latest.

How to add a rule to the Zend URL mapping and compose multiple MVC paths?

This is because I want to develop a web platform with more than one application in the same project.
In any MVC web application we should have this default URL schema:
domain/controller/action/parameters
1: In Zend, what can I do (in which files) to change this schema to add the application name before the controller name?
Expected Result: domain/application/controller/action/parameters
2: How can I implement the consequences of this new URL block in terms that I will separate the MVC for each application, maintaining the shared resources in a separate directory? What changes may I do in Zend autoloader
Expected Result:
/public_html/
/public_html/platform
/public_html/platform/apps
/public_html/platform/apps/base (user interface container)
/public_html/platform/apps/crm
/public_html/platform/apps/crm/model
/public_html/platform/apps/crm/view
/public_html/platform/apps/crm/control
/public_html/platform/apps/crm/public
/public_html/platform/apps/crm/public/css (and etc.)
/public_html/platform/apps/erp
/public_html/platform/apps/erp/model
/public_html/platform/apps/erp/view
/public_html/platform/apps/erp/control
/public_html/platform/apps/erp/public
/public_html/platform/apps/erp/public/js (and etc.)
/public_html/platform/sys
/public_html/platform/sys/core
/public_html/platform/sys/cfg
/public_html/platform/libs/
/public_html/platform/libs/zend
/public_html/platform/libs/template_it
/public_html/platform/libs/custom
i think it is as easy as having actual different ZF2 application, every one in its own folder, and in the same level, a "vendor" folder where you put all the shared structure (coming from zend, third party libraries, etc).
Then inside the vendor folder, i would create another folder for your own shared code, including all your modules that has to be used by more than one of the applications, so your code is a library for yourself.
Since your app is actually in domain/application, and everyone has it own config, it is very straightforward to have domain/application/controller/action/parameters routes: you just create your normal controller/action/parameters routes, since the app actually resides in domain/application/ and the router doesnt have to care about it.
As you noticed, another problem is the autoloader. YOu just need to update the references to the shared modules inside your application.config.php for everyone of your apps
return array(
'modules' => array( //....
),
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php'
),
'config_cache_enabled' => false,
'cache_dir' => 'data/cache',
'module_paths' => array(
'./module',
'../vendor',//reference to your library modules
),
),
//...
);
of course, if the modules doesnt reside directly inside vendor/module but something like vendor/libraryname/module, you have to take a look at your autoload system (Composer autoloading or whatever) and add the classes or namespaces to the corresponding maps.

Check what view file ZF2 is trying to load?

I am building my first ZF2 application, and in one of my modules, the views associated with my controller are not loading. Is there a way I can check what view path ZF is trying to execute?
I have checked all of the file paths and module config settings, and they seem correct, and all of my other modules that have the same layout work fine, so I am thinking this is either a filename or namespace issue.
If you use Zend-developer-tools it shows you in the toolbar which layout, template is used.
To your problem - in module you can replace the template from other modules. But it should depend on template path stack.
In module.config.php, you should have something like:
'view_manager' => array(
'template_path_stack' => array(
'application' => __DIR__ . '/../view',
),

ZF2: how to implement different configs for production, staging etc?

In the sceleton application that I've downloaded from github there is a file
module/Application/config/module.config.php
return array(
'layout' => 'layout/layout.phtml',
'display_exceptions' => true,
'di' => array(
'instance' => array(
'alias' => array(....
this file is used in module/Application/module.php:
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
How to create 3 different configs depending on domain (production, staging, development)? It seems in ZF1 env vars has been used, but I don't know how to do that in zf2 module.
Thank you!
Create a file called development.config.php in application/config/autoload and this will be loaded after all the modules' config files have been loaded. As a result, you can override anything the merged configuration by adding the relevant keys to this file.
The name of the file loaded is {APPLICATION_ENV}.config.php, so you can create production.config.php, etc.
Note that you may have to change the glob in index.php as it's unclear if the Skeleton application will work out of the box with APPLICATION_ENV or not at this stage of the development of ZF2 (early April 2012).
it seems to work with a simple .htaccess change. :
SetEnv APPLICATION_ENV development
I don't know if staging will work, but production and development work out of the box.
I think it works through the event listener, but don't ask me how, I haven't gotten that far yet.

Categories