Codeigniter 4 - Cannot get my module to work - php

I am trying to get a simple module working. All it does is echo 'test' to a web page but I cannot get it working.
The first thing I did was add the module to app/Config/Autoload.php as follows;
public $psr4 = [
APP_NAMESPACE => APPPATH, // For custom app namespace
'Config' => APPPATH . 'Config',
'Modules\Filemanager' => ROOTPATH . 'modules'
];
Then, I created the following directory structure;
In Modules/Filemanager/Config/Routes.php I have added following route;
<?php
$routes->add('/filemanager/(:any)', 'Modules\Filemanager\Controllers\Filemanager::index');
Lastly, in Modules/Filemanager/Controllers/Filemanager I have the following method:
<?php
namespace Modules\Filemanager\Controllers;
use App\Controllers\BaseController;
class Filemanager extends \App\Controllers\BaseController
{
public function index(){
echo 'test'; die();
}
}
When I go to my browser and enter example.com/filemanager/index I get the following error;
Controller or its method is not found: \App\Controllers\Filemanager::index
Any ideas would be much appreciated.

You've done few things wrong here.
In the App/Config/Autoload.php,
you've written 'Modules\Filemanager' => ROOTPATH . 'modules'
But in your directory structure image, there is no directory with the name 'modules'
Please change the 'Modules' folder name to 'modules'
Change 'Modules\Filemanager' => ROOTPATH . 'modules' to
'Modules' => ROOTPATH . 'modules' in App/Config/Autoload.php
In the Modules/Filemanager/Config/Routes.php
You're using the $routes variable in the below line
$routes->add('/filemanager/(:any)', 'Modules\Filemanager\Controllers\Filemanager::index'); But $routes is not defined anywhere in the file.
Put
if(!isset($routes)) {
$routes = App\Config\Services::routes(true);
}
$routes->get('/filemanager/(:any)', 'Modules\Filemanager\Controllers\Filemanager::index');
It should be $routes->get() instead of $routes->add()
Module level routing will not work until you add these routes in the App\Config\Routes.php file.
To add your modules routes to the app\Config\Routes.php
foreach(glob(ROOTPATH . 'modules/*', GLOB_ONLYDIR) as $item_dir) {
if(file_exists($item_dir . '/Config/Routes.php')) {
require_once($item_dir . '/Config/Routes.php');
}
}
After these changes your code should work.

Related

PrestaShop: How to load custom class (models) of my module

Hello i am working on prestrashop its new for and i am trying to develop a module in admin panel i create a model calss sizeguide but when i click on add its showing the error ,
Fatal error: Class 'sizeguide' not found in classes\controller\AdminController.php on line 1614
i did try to fix it but i am not able to fix it even class is showing in class_index.php like this .
'Sizeguide' => array (
'path' => '',
'type' => 'class',
'override' => false,
),
'SizeguideCore' => array (
'path' => 'classes/Sizeguide.php',
'type' => 'class',
'override' => false,
),
please help me to fix this issue thanks in advance.
A moduleadmincontroller it's a little bit different of a modulefrontcontroller.
I'll write some guidelines.
Naming
The class name should be like this AdminCONTROLLERNAMEController.
For example:
class AdminGalleryController
The complete declaration should be:
class AdminGalleryController extends ModuleAdminController
{
// some stuff
}
Add 'controller' in DB
The new admin controller must be added in database, otherwise the dispatcher didn't find it.
This snippet add the new admin controller in the database:
$tab = new Tab();
foreach (Language::getLanguages() as $language) {
$tab->name[$language['id_lang']] = $tabName;
}
$tab->class_name = 'AdminGallery';
$tab->module = /*yourmodulename*/;
$tab->id_parent = 0;
/*
* If you want to add as a child of some admin controller
* that is in the backoffice menu you have to use this code:
* (int)Tab::getIdFromClassName('AdminCatalog');
* With that code you add your controller as a child of 'AdminCatalog' controller
*/
$tab->save()
Usually this snippet should be added in install method of your module.
After that to answer to your question, your class should not be in class_index.php, your model should be placed in your module and be loaded by him.
For example in your module constructor you can add this snippet:
public function __construct()
{
/* ... */
require_once( _PS_MODULE_DIR_ . DIRECTORY_SEPARATOR . $this->name . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . 'Sizeguide.php' );
/* ... */
}

Not able to access module directory

i generated one module which is for web services and module name is "service".
i added code in web.php
'modules'=>[
'service' => [
'class' => 'app\modules\service\service',
],
],
but i am not able to access this module in browser with below URL
http://localhost/projectfoldername/service/login/index
service = Module [ dir : protected/modules/service/service.php]
login = Controller [ dir : protected/modules/service/controllers/loginController.php]
index = action
below is loginController.php code
<?php
namespace app\modules\service\controllers;
class loginController extends \yii\web\Controller
{
public function actionIndex()
{
echo "test";
exit();
// return $this->render('index');
}
}
please help me if i am missing anything.
Controllers directory where your loginController is, should be within module's directory.
protected/modules/service/controllers/loginController.php

How to create a Enom component in Yii framework

I am new to yii and i have to create a yii component for Enom api .I have followed this url Enom application for refrence . It is in core php and i want to implement this in yii as component or module .I have done in this way
put the files interface and class in the yii component folder.
modify the class as mentioned here yii custom component . Now my class name is EnomService and interface name is EnomInterface
i have added these lines also in my class
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
modified the main.php file in config folder:
'import'=>array(
'application.models.*',
'application.components.*',
),
'defaultController'=>'post',
// application components
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'mycomponent' => [
'class' => 'app\components\EnomService',
],
calling in the controller in this way .
public function actionEnom ()
{
echo "asdgsgsag";
$enom = new EnomService('manoj_rudra', 'manoj#41#', false, true);
$enom->debug = true;
$result= Yii::$app->EnomService->checkDomain('systurn', 'com', true);
//$result = $enom->checkDomain('systurn', 'com', true); // This enables domain spinner
echo '<pre>';
var_dump($result);
echo '</pre>';
}
But it is not working . I am not so much familiar with yii custom component . Please help me to create this .
Are you using Yii or Yii2?
If it is Yii, then you could use plenty of other existing extensions to inspire yourself, for example this one: https://github.com/HeavyDots/yii-sms
As for Yii2 you could do something similar, look into already existing extensions for Yii2 on YiiFramework website and you can see how component classes are defined.
I would recommend:
1) Create a new directory inside "components" named "enom"
2) Place inside that directory all your enom files from https://github.com/comdexxsolutionsllc/MoondayFramework/tree/master/engine/enom
3) Create the component class called "Enom.php" inside the directory, something like this:
<?php
// include enom service class
require(dirname(__FILE__).'/class.EnomService.php');
namespace components\enom;
use Yii;
class Enom extends \yii\base\Component
{
// define private property to store service
private $service;
public function init()
{
parent::init();
// init the service
$this->service=new EnomService('manoj_rudra', 'manoj#41#', false, true);
}
/**
* #return EnomService
*/
public function getService() {
return $this->service;
}
}
?>
4) Then in the configuration properly define the component
'enom' => [
'class' => 'app\components\enom\Enom',
],
5) And finally use it like this
Yii::$app->enom->getService()->checkDomain
As I said before, haven't used Yii2 yet so this might need tweaking but could point you on the right path.

Phalcon unable to find class

Hello all im trying to load multiple libraries which is in different folders in library folder using namespaces but i keep getting not found
My directory structure is like this
app/
controllers/
models/
library/
views/
My loader.php is like this
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerNamespaces(array(
'Test\Name' => __DIR__ . "/../library/",
));
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir
)
)->register();
And my basecontroller is trying to call like this
$var = new Test\Name\functions();
and btw the file functions in library is like this
class functions extends Phalcon\Mvc\User\Component
{
public function __construct()
{
}
public function initialize()
{
}
public function checking(){
echo 'checks';
}
}
i Keep getting
Fatal error: Class 'Test\Name\functions' not found in C:\wamp\www\app\controllers\ControllerBase.php on line 38
Any help is appreciated guys thnx
I think that your class should have:
namespace Test\Name;
class functions extends Phalcon\Mvc\User\Component
{
// ... rest of it
on top.
I would also make this configuration:
$loader->registerDirs(
array(
$config->application->controllersDir,
$config->application->modelsDir,
__DIR__ . "/../library/",
)
)->register();
So your class would be in (also I would rename your class to Functions:
app/library/Test/Name/Functions.php
So it would be obvious that your Functions class is in Test\Name namespace.
Having trouble finding the discussion that clued me in but, my understanding is that when using namespaces, you use namespaces. When using directories and other rules, you don't use namespaces.
Namespaces are faster so probably best to just stick with them, dropping the registerDirs as they are superfluous and mean the same thing as the namespaces:
library\Test\Name.php
becomes:
$loader->registerNamespaces(array(
'Apps\Module\Controllers' => $config->application->controllersDir,
'Apps\Module\Models' => $config->application->modelsDir,
'Test' => __DIR__ . "/../library/Test",
));
Then available as Test\Name.
$loader->registerNamespaces(array(
'App' => __DIR__ . "/../library/",
), true);
it will merge all sub dir, if you librarry dir architecture like this
library
- Test
- Name.php
You can call new \App\Test\Name();

Custom layout from public folder in Zend Framework 2

I have templates folder in my public folder.
I want to have opportunity to upload different layouts form my admin panel and put them to public/templates folder.
How can I change standard layout then?
$template = 'christmas.phtml';
$viewModel = new ViewModel();
$viewModel->setTemplate('/../../public/templates/'.$template);
isn't working :(
I also tried this way (changing layout) up from current:
$this->layout('../../public/templates/'.$template);
I found the solution. I've added one line to my module.config.php:
'view_manager' => array(
...
'template_path_stack' => array(
__DIR__ . '/../view',
__DIR__ . '/../../../public' // newLine
),
),
Then simply use this in controller:
$template = 'sometemplate.phtml';
$this->layout('templates/'.$template);
can you try $viewModel->setTemplate(APPLICATION_PATH .'/../public/templates/'.$template); or $viewModel->setTemplate('./../../public/templates/'.$template); instead of $viewModel->setTemplate('/../../public/templates/'.$template);. Because I guess you want to go up from the application directory, not from the root directory and $template = 'christmas.phtml'; should be $template = 'christmas';
If there is no APPLICATION_PATH constant you can define it in index.php file. If you dont like to define it, you can use realpath('<path/to/your/public/templates/>') method.

Categories