Zend: Where can i place custom classes in zend folder structure? - php

I want to put projecct dependent custom class in zend folder structure. I googled and found that we can put it in models folder, but we use models folder for database related classes. So I am confuse. Please help where can i put it so i can access it by directory mapping?

This is my way:
I create in library path, folder 'My'.
There I put file Utils.php.
So in Utils.php I have class
class My_Utils { /* some functions... */ }
in configs/application.ini autoload namespace 'My' with this line:
autoloaderNamespaces.my = "My_"
So in controller you can use that class like
$utils = new My_Utils();
$utils->someFunction();
Other way, you can autoload that folder(My) in bootstrap
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' => '../library/My/',
'namespace' => 'My_'
),
),
)
);
return $resourceLoader;
}

Look up autoloading in Zend.
This will let you define your own application folder and then include the ZF library.
From this application folder you can do as you wish when it comes to folders and classes and the autoloader will load them all for you without the need for includes or requires.

Related

Codeigniter 4: creating modules

I'm trying to create modules in Codeigniter 4 to work with HMVC. I tried following this user guide https://codeigniter4.github.io/userguide/general/modules.html, but cannot get it working.
I created a 'modules' folder, alongside the app, public, etc. folders.
Added to app/config/autoload.php
'Modules' => ROOTPATH.'modules'
Inside the modules folder, I created a 'Proef' folder, containing a Controllers folder and 'Proef.php' file.
The file contains the following;
namespace App\Modules\Proef\Controllers;
class Proef extends \CodeIgniter\Controller
{
public function index() {
echo 'hello!';
}
}
In the app/config.routes.php file I added
$routes->group('proef', ['namespace' => 'Modules\Proef\Controllers'], function($routes)
{
$routes->get('/', 'Proef::index');
});
Yet, the following error persists:
Controller or its method is not found: \Modules\Proef\Controllers\Proef::index
What am I missing?
If you put your modules folder "alongside" and not under your app folder, then your namespace is wrong.
So you would have something like
app/
Modules/ ==> can be modules or Modules but must be set in autoload with the same case
Proef/
Controllers/
Proef.php
NOTE: modules can be Modules or modules but the corresponding entry in the autoload must match.
For modules
'Modules' => ROOTPATH . 'modules'
For Modules
'Modules' => ROOTPATH . 'Modules'
It appears (from my limited testing) that the other folder names must
be 1st letter Upper case. This is under Apache on Linux.
let's use Modules for the folder name so in Autoload.php we would have...
$psr4 = [
'App' => APPPATH, // To ensure filters, etc still found,
APP_NAMESPACE => APPPATH, // For custom namespace
'Config' => APPPATH . 'Config',
'Modules' => ROOTPATH . 'Modules'
];
So your Proef Controller - Proef.php ... Note the namespace being used.
<?php
namespace Modules\Proef\Controllers;
use App\Controllers\BaseController;
class Proef extends BaseController {
public function index() {
echo 'Hello - I am the <strong>'. __CLASS__ . '</strong> Class';
}
}
To make this accessible via the URL you can set the routes (Routes.php) to... (simple version)
$routes->get('/proef', '\Modules\Proef\Controllers\Proef::index');
To make it callable within other Controllers... ( I have borrowed Home.php for this)
<?php namespace App\Controllers;
use \Modules\Proef\Controllers\Proef;
class Home extends BaseController
{
public function index()
{
$mProef = new Proef();
$mProef->index();
return view('welcome_message');
}
//--------------------------------------------------------------------
}
In your URL -
/proef will result in the just the message
/home will result in the class message and the welcome page.
So hopefully this will help you figure this out. its a lot of fun :)
Aside:
You can put your Modules Folder anywhere. I put mine under app/ for ole times sake, which removes the need to add the entry in Autoload.php as they fall under app/ which is already defined.
The namespace and use statement need to be changed appropriately as well.
Edit
namespace to Modules\Proef\Controllers in Proef class

Correct way to access Yii2 components in your modules?

I have created and configured a module fooModule. I need to create a component inside the module.
This is my configuration for my module in main.php
'modules'=>array(
'fooModule'=>array(
'class' => 'app\modules\fooModule\Module',
'components'=>array(
'testComponent'=>array(
'class'=>'app\modules\fooModule\components\testComponent',
),
),
),
),
In the folder module fooModule i have created a folder components with a file testComponent.php
TestComponet.php has a class test which extend \yii\base\Component. See below
namespace app\modules\fooModule\component;
class test extends \yii\base\Component {
public function __construct() {
private $bar;
}
public function exampleFunction() {
echo 'am alive, come and look for me please!!';
}
}
How do i access test class in fooModule Controller ?
Use Yii::$app->getModule('fooModule')->testComponent->exampleFunction(); for access module component.
The most elegant way to access module component from module controller without hardcoding module's ID is as follows:
$this->module->testComponent->exampleFunction();
This looks very elegant
Module::getInstance()->testComponent

Access config global in Zend Framework 2

I would like to access to my global configs (config/{,*.}{global,local}.php) located in my personal libraries (in the vendor directory).
But despite my search in the web, I did not succeed to achieve this.
I know how to access the config file of a module :
\MyModule\Module::getConfig()
I know how to access the global configurations from a controller :
$this->getServiceLocator()->get('config');
But how to access these from a file inside the vendor directory ?
My libraries do not extend anything, it may be the root of the problem ?
Can I use a static method to access these ?
Thanks.
You can get it through the serviceManager.
In controller:
$config = $this->getServiceLocator()->get('Config');
Otherwise same way, just you need service Manager, for eaxmple in Module.php:
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'mail.transport' => function($sm) {
$config = $sm->get('Config');
switch($config['mail']['transport']['type']){
..................
easy way to do this. not only in module but anywhere.
$config = new \Zend\Config\Config( include APPLICATION_PATH.'/config/autoload/global.php' );
echo $config->myconfig;
define APPLICATION_PATH in public/index.php

Putting models in library directory in Zend Framework

I want to put models outside module directory in Zend Framework. To be precise, in /library folder
library/
models/
actors/
ActorsMapper.php
Actor.php
books/
BooksMapper.php
Book.php
INSTEAD OF
application/
modules/
models/
actors/
ActorsMapper.php
Actor.php
books/
BooksMapper.php
Book.php
This is done so that I don't have to create separate model for each of the module I create.
What configurations will I have to change?
If you need more details, please ask.
Thank you :)
First answer works, but I will give you another answer in case if you want to register autoload in bootstrap.
1) put 'models' folder into library with all Table.php files.
Every model/class should have:
class Model_Table extends Zend_Db_Table_Abstract{ ... }
2) In bootstrap.php put:
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' => '../library/models/',
'namespace' => 'Model_'
),
),
)
);
return $resourceLoader;
}
That's it. Now you can use your models in controllers like this:
$model = new Model_Table();
If you want to use same models for all modules you can put it in application folder applications/models
and that works fine just like when you have web without modules.
But If you want to have models in library, You can put 'models' folder in your library path and autoload it.
Create folder 'Models' into library with all Table.php files.
In configs/application.ini Put:
autoloaderNamespaces.models = "Models_"
Then you can use namespace 'Models_' in your web application
In controller:
$model = new Models_Table();
in any case, I recommend that you keep the folder models in the path application/models
zend autoloader loading class which name beginning with prefix, that is registred on it.
If you have library in your include path, you can simple register "Models" like default namespace on autoloader and name your class for example Models_Actors_Actor.

Access my ZF models (application/models) without a prefix/namespace?

My Zend Framework setup is like this:
/application
/modules
/models
/configs
/library
/public
I want to access my models without needing prefixes or namespaces, like this:
new User() // Would refer to /application/models/User.php
I know this is a fairly simple problem, but I havn't been able to figure it out yet. I've also seen a lot of similar questions but none that I thing were this one exactly, but please forgive me if I am duplicating an existing question.
So far, I have tried changing appnamespace to "" in my config.ini with no success and adding
the following to my Bootstrap with no success:
protected function _initAutoload()
{
$autoloader = new Zend_Loader_Autoloader_Resource(array(
'namespace' => '',
'basePath' => APPLICATION_PATH . '/models',
));
}
Thanks!
You have to use the fallback autoloader..
In your Bootstrap file (or wherever you like), do this:
protected function _initAutoloader()
{
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
}
That will allow you to load any arbitrary class that is in your path, even if you've registered specific autoload prefixes in your application.ini
And to clarify, make sure you are pushing APPLICATION_PATH . "/models" into your include_path at some point..

Categories