Phalcon Incubator not loading translation namespace - php

I have have added the composer require:
{
"require": {
"phalcon/incubator": "dev-master"
}
}
I've added the location to the Phalcon loader file (is this correct?):
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerNamespaces(array(
'Phalcon' => __DIR__ . '/../../vendor/phalcon/incubator/Library/Phalcon/',
'APPNAME\Models' => $config->application->modelsDir,
'APPNAME\Controllers' => $config->application->controllersDir,
'APPNAME\Forms' => $config->application->formsDir,
'APPNAME' => $config->application->libraryDir
));
$loader->register();
Then I have this in my controller:
$translate = new Phalcon\Translate\Adapter\Gettext(array(
'locale' => 'en_GB',
'file' => 'messages',
'directory' => '../app/lang'
));
But I get the following error:
Why isn't it loading the Incubator files?
Thanks
Nathan

You need to use FQCN when you are not including namespaces using the use keyword: (not the first \ in the FQCN)
$translate = new \Phalcon\Translate\Adapter\Gettext(array(
'locale' => 'en_GB',
'file' => 'messages',
'directory' => '../app/lang'
));
If you don't do this, the namespace is assumed to be relative to the current namespace (which is defined by the namespace statement at the top of the file)
Btw, you don't need to configure the Phalcon autoloading, composer takes care of the autoloading of Phalcon for you. I would also recommend using Composer's autoloader instead of both Composer's and phalcon's loaders.

Related

Loading Modules which has multiple module in Zend Framework 2

Issues
So I try to decouple my application to multiple module project ( each has its own composer.json ), then the real application will load all this project through composer
Each of this module project will have a user-interface accessible through browser and can start individually, so it's not just a simple library. This file will exist on each module project:
config/application.config.php
public/index.php
Example Module ( Dependency is what I write in module array in application.config.php ):
UIModule
Dependency : AssetManager, UIModule
CMSModule
Dependency : UIModule, CMSModule
AccountingModule:
Dependency : UIModule, AccountingModule
Now in my final application lets say MyApplication it need both CMSModule and AccountingModule, but I cannot write only just this two module in application.config.php. Instead I have to write:
AssetManager -> this should be load by UIModule
UIModule -> this should be load by CMS/Accounting Module
CMSModule
AccountingModule
I should only require to write this two in MyApplication
CMSModule
AccountingModule
Is this can be done ? which I think what this guy want to achieve in Loading Modules Dynamically in Zend Framework 2
Something like this, I add another additional module.
Based on our exchange in the comments and the question, you're going to need at least 3 applications. I'll give you a quick examples, you'll have to update your requirements for each application yourself. After the composer.json configs I'll give you a skeleton module to use as a theme module.
These config's are to be used as the root composer.json config files. Each of the required packages should have their own composer file listing requirements for the specific package.
For example, a "core" module would require various Zend Framework packages. A "theme" package could be requiring other ZF packages, such as zendframework/zend-view in order to be able to have a GUI layout.
Setting up 3 separate Zend Framework applications with overlapping requirements
composer.json for application 1
{
"name": "COMPANY_NAME/APPLICATION_1",
"require": {
"COMPANY_NAME/MODULE_1_THEME": "*",
"COMPANY_NAME/MODULE_2_CMS": "*"
},
"repositories": [
{
"type": "git",
"url": "git#github.com/COMPANY_NAME/MODULE_1_THEME.git"
},
{
"type": "git",
"url": "git#github.com/COMPANY_NAME/MODULE_2_CMS.git"
},
]
}
composer.json for application 2
{
"name": "COMPANY_NAME/APPLICATION_2",
"require": {
"COMPANY_NAME/MODULE_1_THEME": "*",
"COMPANY_NAME/MODULE_3_ACCOUNTING": "*"
},
"repositories": [
{
"type": "git",
"url": "git#github.com/COMPANY_NAME/MODULE_1_THEME.git"
},
{
"type": "git",
"url": "git#github.com/COMPANY_NAME/MODULE_3_ACCOUNTING.git"
},
]
}
composer.json for application 3 (has no theme)
{
"name": "COMPANY_NAME/APPLICATION_3",
"require": {
"COMPANY_NAME/MODULE_4_AUTH_MODULE": "*"
},
"repositories": [
{
"type": "git",
"url": "git#github.com/COMPANY_NAME/MODULE_4_AUTH_MODULE.git"
}
]
}
As you can see, the Applications 1 & 2 use the same MODULE_THEME package, as you outlined in the diagram in your question.
Now, the creation of a package for Zend Framework is pretty much the same for every package you create, so modify what follows to the requirements you have for each module (in a package).
Creating a theme module
This module basically replaces the Application module that you get by default when you install the Zend Framework (2 or 3) Skeleton Application.
I've recently upgraded everything I have with Zend Framework to Zend Framework 3, so I'll be giving you a setup tailored for ZF3. However, downgrading for ZF2 should not be too much of an issue.
Create config for what you need
A typical theme needs a few things, such as:
themes/layouts for different types of pages (e.g. login, normal theme, errors)
translations
showing errors (when in dev mode)
default "home" route
controller to handle default "home" route
Config for this could be (not limited to! Do with it what you wish!) as such in the module.config.php of the Theme module
namespace COMPANY_NAME\Theme;
use COMPANY_NAME\Theme\Controller\ThemeController;
use COMPANY_NAME\Theme\Factory\ThemeControllerFactory;
return [
'controllers' => [
'factories' => [
ThemeController::class => ThemeControllerFactory::class,
],
],
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'may_terminate' => true,
'options' => [
'route' => '/',
'defaults' => [
'controller' => ThemeController::class,
'action' => 'index',
],
],
],
],
],
'route_layouts' => [
'*' => 'layout/layout',
'login' => 'layout/login',
'register' => 'layout/login',
'error*' => 'error/index',
'error/404' => 'error/404',
],
'translator' => [
'locale' => 'en_US',
'translation_file_patterns' => [
[
'type' => 'gettext',
'base_dir' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'language',
'pattern' => '%s.mo',
],
],
],
'view_manager' => [
// controller_map is optional, but depending on your composer package nesting, could be a great help. Have a look here for how to use: https://blog.alejandrocelaya.com/2015/08/14/working-with-sub-namespaced-modules-in-zend-framework-2-the-right-way/
'controller_map' => [
'COMPANY_NAME\Theme' => 'company_name_path_alias',
],
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => [
'layout/layout' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR .
'layout' . DIRECTORY_SEPARATOR . 'layout.phtml',
'layout/login' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR .
'layout' . DIRECTORY_SEPARATOR . 'login.phtml',
'error/404' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR .
'error' . DIRECTORY_SEPARATOR . '404.phtml',
'error/index' => __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'view' . DIRECTORY_SEPARATOR .
'error' . DIRECTORY_SEPARATOR . 'index.phtml',
],
'template_path_stack' => [
__DIR__ . DIRECTORY_SEPARATOR .'..' . DIRECTORY_SEPARATOR . 'view',
],
],
];
File/module structure based on config
The location of the package would be /vendor/COMPANY_NAME/THEME_MODULE_NAME (as you would've defined in the name property in the composer.json file for this package.
The folder/file structure would be:
/vendor/COMPANY_NAME/THEME_MODULE_NAME
config/
module.config.php
src/
Controller/
ThemeController.php
Factory/
ThemeControllerFactory.php
Module.php
view/
error/
index.phtml
404.phtml
layout/
index.phtml
login.phtml
register.phtml
composer.json
ThemeController & *Factory
These are very simple as the Controller is pretty much a clone of the original IndexController provided by the Skeleton Application. The Factory in this instance does nothing but return the Controller. As such you could replace the config for it with the FQCN to the InvokableFactory of Zend Framework 3 and not make the Factory class. However, if your ThemeController needs some requirements (such as a RegisterForm), you're going to need the Factory to provide these.
ThemeController
namespace COMPANY_NAME\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class ThemeController extends AbstractActionController
{
public function indexAction()
{
return [];
}
}
ThemeControllerFactory
namespace COMPANY_NAME\Factory;
use COMPANY_NAME\Controller\ThemeController;
use Zend\ServiceManager\Factory\FactoryInterface;
class ThemeControllerFactory implements FactoryInterface
{
/**
* #param ContainerInterface $container
* #param string $requestedName
* #param array|null $options
* #return ThemeController
* #throws \Psr\Container\ContainerExceptionInterface
* #throws \Psr\Container\NotFoundExceptionInterface
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new ThemeController();
}
}
Theme composer requirements
Obviously your modules will not have the same requirements. Make sure you figure out what they are, per module.
For my own Theme module, I have the following Zend Framework requirements in my composer.json file:
{
"name": "COMPANY_NAME/THEME_MODULE_NAME",
"require": {
"zendframework/zend-di": "*",
"zendframework/zend-navigation": "*",
"zendframework/zend-view": "*",
}
}
In the require section I also have: "rwoverdijk/assetmanager": "^1.6",. This module is used to mash together all CSS, JS (any type really) of file to a determined location. I would advise you to have a look at it (here).
Notes on the answer
Replace COMPANY_NAME with the username of your Github account (or the identifying account name if your using Bitbucket or Gitlab)
Replace THEME_MODULE_NAME with the name of the repository
If/when possible, use explicit versions for required packages (e.g. "rwoverdijk/assetmanager": "^1.6"). Version locking can save you a lot of hassle in the future...
Additionally: using a package as a "Theme module" allows you to completely remove the module/ folder originally shipped with the Skeleton Application of Zend Framework. However, you're hereby advised to use the module/ folder for application specific modules. If you create a package for everything, you'll soon find yourself maintenance hell.
Yep your layout is what I came at the end
However, you're hereby advised to use the module/ folder for application specific modules.
Kind of, I end up putting inside a folder for every specific package ( zf2 style )
PACKAGE FOLDER
composer.json
Module.php (this for php unit test)
public (for UI Package I have this)
index.php
config
application.config.php (apparently need to write each version for each package)
tests
src
MODULE_NAME
asset
src
MODULE_NAME
Controller
Service
Model
{ Other ... }
config
view
Module.php
Thanks for your clarification and answer.

Yii2 change default Route to another namespace

the default route in Yii2 is #app/controllers/SiteController.
but If I build from beginning.
composer require yiisoft/yii2
then I create my own index.php and set the configure for the app
(new yii\web\Application($config))->run();
and the application is always try to local the defaultContoller in the app namespace. but If i put the SiteController in another namespace. it gives me 404 error,
index.php
require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/repo/config/web.php');
(new yii\web\Application($config))->run();
web.php
'id' => 'repo',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'repo\\controllers',
'defaultRoute' => 'site/index',
folder structure
/vendor
-yiisoft
/repo
-config
---web.php
-controllers
---SiteController.php
SiteController.php
<?php
namespace repo\controllers;
use Yii;
use yii\web\Controller;
class SiteController extends Controller{
public function actionIndex(){
echo 'welcome to the site';
}
}
?>
if I give the SiteController.php namespace app/controllers it works,but once I change it back to repo/controllers it says page not found.
try this in you config file:
'controllerNamespace' => 'newnamespace\controllers',
'defaultRoute' => 'new/index',
yii\base\Application object has a public property controllerNamespace, which defaults to app\\controllers, you need to change it accordingly to changing default controller namespace.
Add this to your application config:
[
...
'controllerNamespace' => 'app\\other\\namespace\controllers',
...
],
Default route is site (for yii\web\Application), it can be changed the same way (defaultRoute property):
[
...
'defaultRoute' => 'otherDefaultRoute',
...
],
I found I forgot to define the root alias.
// $className is a fully qualified class name without the leading backslash
$classFile = Yii::getAlias('#' . str_replace('\\', '/', $className) . '.php');
so If I define the root alias #repo in the configuration. then it works
'aliases' => [
'#repo' => dirname(__DIR__),
],

ZF2 Routes - The requested URL could not be matched by routing

I'm having trouble setting up a route for a very simple controller. I'm getting the "The requested URL could not be matched by routing." error. I've viewed similar solved questions on SO and can't pinpoint what I'm doing wrong (Ex: ZF2 - Zend Framework 2, understanding routing)
I've followed the skeleton tutorial with the albums subject and everything functioned perfectly fine. I tried duplicating the album module and then changing the name of the controller, folder, module config, etc. I figured this would be a good way to confirm I can at least replicate working code. I'm just trying to echo "123" to the page, so I tried eliminating the directories for forms, models and some of the views from the new module.
Is there some way to see what route I'm really looking for and what routes I defined? I know CI actually created a log file I was able to check. It was kind of like Apache logs but specific to framework functionality.
I'd like to post some of my code so someone could point out the mistake I am making and possibly explain why it is wrong. I tried paying close attention to case since different variations of the word album are used throughout the tutorial and I'm not 100% sure which ones are supposed to match up with what just yet. I'm trying to make it work for http://www.example.com/productbriefs.
Folder Structure
module.config.php:
return array(
'controllers' => array(
'invokables' => array(
'Productbriefs\Controller\Productbriefs' => 'Productbriefs\Controller\ProductbriefsController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'productbriefs' => array(
'type' => 'Literal',
'options' => array(
'route' => '/productbriefs',
'defaults' => array(
'controller' => 'Productbriefs\Controller\Productbriefs',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'productbriefs' => __DIR__ . '/../view',
),
),
);
ProductbriefsController.php
namespace Productbriefs\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class ProductbriefsController extends AbstractActionController
{
public function indexAction()
{
echo "123";
}
}
Module.php
namespace Productbriefs;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements AutoloaderProviderInterface, ConfigProviderInterface
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
// Add this method:
public function getServiceConfig()
{
return array(
'factories' => array(),
);
}
}
As per my comment, you need to add Productbriefs to the module array in application.config.php or the module (including its configuration) will not be loaded.
To answer your second question, the controller manager needs to know how to load the controller classes your application uses. An 'invokable' is a class that can be instantiated without needing any arguments passed to it, so by adding controllers to that array you're telling the controller manager that it can instantiate that class simply by doing $controller = new Productbriefs\Controller\ProductbriefsController(). The key for the array is an alias, yes. This can be anything, although the ZF convention is to use the fully qualified name of the class but omit the 'Controller' suffix from the end. When you refer to controllers in your routing config you use these aliases.

Zend Framework 2 including custom library

My directory structure is like this:
c:\Workspaces\Zend
c:\Workspaces\Custom library
Custom library is a shared library, which is in use in other applications. It doesn't use namespaces, just old style underscores.
I downloaded the ZF2-Restful-Module-Skeleton which i intend to use as a restful server. In the InfoController I have this code:
namespace Main\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
class InfoController extends AbstractRestfulController
{
public function getList()
{
$data = array(
'phone' => '+30123456789',
'email' => 'email#domain',
);
$Res = new CL_Res_Chain_Mutable();
return $data;
}
}
Error message is:
Fatal error: Class 'Main\Controller\CL_Res_Chain_Mutable' not found in C:\Workspaces\Zend\module\Main\src\Main\Controller\InfoController.php
Obviously, I need to add this custom library to my Zend application, but Im "little" lost here, I really don't know how to do this. I have googled couple solutions, but none of them seem to be like this.
Also, I have another library in folder c:\Workspaces\Custom library 2, which has (among other files) file(class) D.php, which I have used like D::dump($data);
How can I get it to work in my Zend application like that?
You need to configure the StandardAutoloader to load your library classes. The easiest way is to modify the Application module's Module::getAutoloaderConfig() method so that it looks something like this:
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
'prefixes' => array(
'CL' => 'c:\\Workspaces\\Custom library/CL',
'D' => 'c:\\Workspaces\\Custom library 2/D',
),
),
);
}
I've added a prefixes key and then listed the prefix name and where to find it on disk. The Standard Autoloader documentation has more details.
If you are working with a Zend Skeleton Application you may also simply add these namespaces to your init_autoloader.php file.
The namespace of your class is Main\Controller. If you instanciate a new class here new CL_Res_Chain_Mutable() php will load it relative to the current namespace Main\Controller\CL_Res_Chain_Mutable. Your class is not a namespaced class so you need to load it from the root. Just put a \ in front new \CL_Res_Chain_Mutable().
By default your application will be using the Standard Autloader (PSR-0). This will find your files based on a namespaces, and a naming convension used by ZF2.
ZF2 will allow you to register multiple Autoloaders, so you can use different strategies, which is what you will need to do, here's an example:
Module.php
/**
* Get autoloader config
*
* #return array
*/
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
// File containing class map key/value pairs
__DIR__ . '/library/autoloader_classmap.php',
// Or provide an array with the class map instead...
array(
'Application\Bootstrap' => __DIR__ . '/application/Bootstrap.php',
'CL_Res_Chain_Mutable' => __DIR__ . '/library/pathhere/Mutable.php',
),
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
This setup will use tell ZF2 to check the class map first, if it can't find what it's looking for it will revert to the standard autoloader.
http://framework.zend.com/manual/2.0/en/modules/zend.loader.class-map-autoloader.html

addResourceType - how can I add Models_Mapper as a resource?

I'm writing a PHP application using
PHP 5.3 and Zend Framework 1.11.7.
I created model resource using the following command:
protected function _initLoader()
{
$loader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'Default',
'basePath' => APPLICATION_PATH,
));
$loader -> addResourceType ( 'model', 'models', 'Model');
}
now whenever i use a class name that starts with Default_Model_ it goes and searches in models directory. under models directory i have a directory called mapper.
how can I configure that whenever a class is being used that starts with Default_Model_Mapper_ to auto-load it from models/mapper ?
ZF should find any directory under models automatically. Also you shouldn't need to include the default module either.
Model_
Model_Mapper_
Also to setup ZF modules I have the following 2 lines of code in my application.ini file
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] =
And the following in my bootstrap
protected function _initModuleAutoload()
{
$modelLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/modules/default'));
return $modelLoader;
}
I hope this is of some help
Kind regards
Garry
Add this one:
$loader->addResourceType('mapper', 'models/mapper', 'Model_Mapper_');
The order in which you declare the two resource types might matter. So try it both ways; one of them should work.
It is set by default in Zend_Application_Module_Autoloader in initDefaultResourceTypes:
$this->addResourceTypes(array(
'dbtable' => array(
'namespace' => 'Model_DbTable',
'path' => 'models/DbTable',
),
'mappers' => array(
'namespace' => 'Model_Mapper',
'path' => 'models/mappers',
),
'form' => array(
'namespace' => 'Form',
'path' => 'forms',
),
....

Categories