using Doctrine With laminas project issue : "Class does not exist" - zend-framework

I just installed Doctrine with my Laminas project. I added a database connection and generated my entities in the Application folder (Application\Entity). I added my entities settings to the module.config.php file in the Application folder. I created a new User and everything, including routing, is working fine. However, when I try to fetch all data from my table to view, the screen displays an error: 'Class 'Application\Entity\User' does not exist'.
i can't find any solution
module
|_Application
|_config
|_Entity
|_src
|_view
|_User
|_config
|_src
|_view
entities settings : namespace : Application
<in Application folder - module.config.php>
'doctrine' => [
'driver' => [
__NAMESPACE__ . '_driver' => [
'class' => AnnotationDriver::class,
'cache' => 'array',
'paths' => [__DIR__ . '/../Entity']
],
'orm_default' => [
'drivers' => [
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
],
],
],
],
````
**UserManager**
```
namespace : User\Manager
```
```
public function listUsers()
{
$query = $this->entityManager->getRepository(User::class)->findAll();
return $query;
}
```
**UserController**
```
namespace : User\Controller
```
```
public function listUserAction()
{
$data = $this->usermanager->listUsers();
var_dump($data);
die();
$view = new ViewModel([
'users' => $data
]);
return $view;
}
```
`Entity Folder => Path : Application\Entity`

As this question was already answered in the Laminas forums, I think I should complete this question here, too.
Your folder structure is wrong. The folder where your entities are stored must be a subfolder of the src folder of your application.
./module/Application/src/Entity/User.php
In Laminas projects all business logic is stored in the src folder. The Laminas autoloading will try to receive all classes from there.

Related

Configure full screen backend module for typo3 custom extension

I am new to typo3 extension development, i have created extension with extension_builder as well as backend module too.
ext_tables.php
if (TYPO3_MODE === 'BE') {
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerModule(
'USER.Webuser',
'web', // Make module a submodule of 'web'
'bewebuser', // Submodule key
'', // Position
[
'Users' => 'list, show, new, create, edit, update, delete',
],
[
'access' => 'user,group',
'icon' => 'EXT:' . $extKey . '/Resources/Public/Icons/user_mod_bewebuser.svg',
'labels' => 'LLL:EXT:' . $extKey . '/Resources/Private/Language/locallang_bewebuser.xlf',
]
);
}
Typoscript :
# Setting up template
module.tx_webuser_web_webuserbewebuser {
persistence {
storagePid = {$module.tx_webuser_bewebuser.persistence.storagePid}
}
view {
templateRootPaths = EXT:webuser/Resources/Private/Backend/Templates/
partialRootPaths = EXT:webuser/Resources/Private/Backend/Partials/
layoutRootPaths = EXT:webuser/Resources/Private/Backend/Layouts/
}
}
Its working file. here is my BE module:
But, i want to create full area including page tree. Can anyone tell me how to remove page tree for my custom extension use? I want to use entire area for my custom extension.
Thanks an advance!
After taking a look into the source, it seems you can add the option 'navigationComponentId' => '', to the last argument of registerModule to get what you want.
Edit: 2021-02-10. For TYPO3 10 you need to additionally add 'inheritNavigationComponentFromMainModule' => false to the list. I'd assume that only applies if the main module (web in this case) has the page tree activated.
In your example it would be:
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::registerModule(
'USER.Webuser',
'web', // Make module a submodule of 'web'
'bewebuser', // Submodule key
'', // Position
[
'Users' => 'list, show, new, create, edit, update, delete',
],
[
'access' => 'user,group',
'icon' => 'EXT:' . $extKey . '/Resources/Public/Icons/user_mod_bewebuser.svg',
'labels' => 'LLL:EXT:' . $extKey . '/Resources/Private/Language/locallang_bewebuser.xlf',
'navigationComponentId' => '',
'inheritNavigationComponentFromMainModule' => false,
]
);

Set Module name, Controller name, and Action name in Zend framework?

I recently started programming with Zend Framework.I want to change module name, controller name and action name of a module in my framework through coding, while coding in the same framework.
Its name is Application(module & controller) and I want to change it to Institute. Is this possible through coding?
I searched through Google for help, but either i couldn't find it or understand it properly. Any help would be appreciated.
This is really just a case of renaming things:
Update all namespaces from Application to Institute in all the classes in the module including the Module.php
Update the name of the controller and it's entry in config/module.config.php
Make sure you update the name of your view directory if you have one in the module, ie view/application/index etc to view/institute/index and make sure you update the entry in module.config.php to the same path
Update name of Application directory to Institute
Update the name in the array of modules in modules.config.php or if you are using an earlier version application.config.php from under the modules key.
That's all I can think of you would need to do
******** EDIT ********
So the basic idea would be as follows:
Add a console in a new module (I've used zend mvc console but you should probably use https://github.com/zfcampus/zf-console as mvc console is deprecated)
module.config.php
<?php
namespace Rename;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'console' => array(
'router' => array(
'routes' => array(
'rename-module' => array(
'options' => array(
'route' => 'module rename <moduleNameOld> <moduleNameNew>',
'defaults' => array(
'controller' => Controller\IndexController::class,
'action' => 'rename'
)
)
)
)
)
),
'controllers' => [
'factories' => [
Controller\IndexController::class => InvokableFactory::class,
],
],
];
IndexController.php
<?php
namespace Rename\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Console\Request as ConsoleRequest;
use Zend\Mvc\Console\Controller\AbstractConsoleController;
class IndexController extends AbstractConsoleController
{
public function renameAction()
{
$request = $this->getRequest();
// Make sure that we are running in a console and the user has not tricked our
// application into running this action from a public web server.
if (!$request instanceof ConsoleRequest) {
throw new \RuntimeException('You can only use this action from a console!');
}
$moduleNameOld = $request->getParam('moduleNameOld');
$moduleNameNew = $request->getParam('moduleNameNew');
$module = file_get_contents(getcwd() . "/module/$moduleNameOld/src/Module.php", 'r+');
$updatedModule = str_replace($moduleNameOld, $moduleNameNew, $module);
file_put_contents(getcwd() . "/module/$moduleNameOld/src/Module.php", $updatedModule);
rename("module/$moduleNameOld", "module/$moduleNameNew");
}
}
This can be run like
php public/index.php module rename Application Institute
I've only done renaming the module here and renaming the namespace in the Module.php
to finish this off you would need to recursively find all .php files in the Application directory and loop over applying the string replace (which should be improved really). Then you could update the view and application level config too with similar logic and using the steps above.
The code I've written is pretty bad and probably insecure but might help you along the way

how to access local.php file from controller in zf3

I am working with Zend-framework 3. I want to access the content of config files (local.php) in the controller. please explain. Thank you all in advance.
After some research, I found the simplest way to access any config file. Just in your Module.php pass $container->get('config') as one of the argument to your controller's constructor and voila you can now access any property in the controller. That much simple. :-) Happy coding..!!
You can do something like this (it's not tested):
// config.php
return [
'webhost' => 'www.example.com',
'database' => [
'adapter' => 'pdo_mysql',
'params' => [
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase',
],
],
];
And in your controller:
// Consumes the configuration array
$config = new Zend\Config\Config(include 'config.php');
// Print a configuration datum (results in 'www.example.com')
echo $config->webhost;
Presume, you have this ZF3 package: zend-config
If you dont have, you have to include it via composer
composer require zendframework/zend-config

Yii2 restful not working

By using this link I tried to have a restful app, but it's not working and always returned this:
Object not found!
The requested URL was not found on this server. If you entered the URL manually please > > check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.4 (Win32) OpenSSL/1.0.1e PHP/5.5.1
I call it like this: localhost/restful/web/users
Edit:
This kind of call is not working either: localhost/restful/web/?r=users
It returned this:
Not Found (#404)
Page not found.
The above error occurred while the Web server was processing your request.
Please contact us if you think this is a server error. Thank you.
Have the same error (stil getting 404 Not found on each request) have solve it adding .htaccess to (my yii path)/web folder (the same where you have index.php):
# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php
This is because when you create an clean instance of Yii2 you don't have the .htaccess file (in my opinion probably the security reason).
for me the: (enabling curl and using it in my controller) installed via composer
composer require --prefer-dist linslin/yii2-curl "*"
use in controller
<?php
namespace app\controllers;
use yii\rest\ActiveController;
use linslin\yii2\curl;//this was added
class UserController extends ActiveController
{
Don't work.
Maybe this helps someone :)
This is my config/web.php:
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'urlManager'=>[
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
],
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'a',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
],
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';
}
return $config;
And the controller:
<?php
namespace app\controllers;
use yii\rest\ActiveController;
class UserController extends ActiveController{
public $modelClass = 'app\models\User';
}
Also a created my model by gii.
The problem solved by enabling curl and using it in my controller.

Zend 2 Plugin Manager cant find class

Im new to Zend 2 and are trying to migrate my Zend 1 project to Zend 2. I had an Acl plugin in my Zend 1 project that I shared with several apps using symlink. I thought now that I migrate to Zend 2 I'd create my own package in the Vendor folder. I downloaded the Skeleton project and tried to add my plugin as this:
in the vendor folder I create myname\commons\Acl and added a my Module.php
in myname\commons\Acl i created src\WebAcl\Controller\Plugin and a added WebAclPlugin.php with the namespace WebAcl\Controller\Plugin
In my myname\commons\Acl I created ./config and added module.config.php with the content
return array(
// added for Acl ###################################
' controller_plugins' => array(
'invokables' => array(
'WebAclPlugin' => 'WebAcl\Controller\Plugin\WebAclPlugin',
)
),
// end: added for Acl ###################################
);
When I run this I get:
Fatal error: Class 'WebAcl\Controller\Plugin\WebAclPlugin' not found in AbstractPluginManager.php on line 170
What am I doing wrong?
Edit: If I in my module specify the classmap it works
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
)
But if I use "autoload" it doesnt work
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
Edit 2: This solved the problem:
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' *.str_replace("\\", "/", __NAMESPACE__),*
),
),
Still Im trying to figure out what composer.phar actually does? See additional question:
Additional question: I read that I should add my namespace in composer.json and run composer.phar update, which adds it to auto_namespace. I did this, but do I need to when I specify it in my module? Sorry if my questions are stupied.
The plugin manager will try to load the class using new which will make the registered autoloaders try to load the class. If there isn't an autoloader that can load this class, then you'll get the fatal error.
You don't say if myname\commons\Acl is a ZF2 module or a composer loaded package.
If it's a composer package, then you need to add:
"autoload": {
"psr-4": {
"WebAcl\\": "myname/commons/Acl/src/WebAcl"
}
}
to composer.json and then run composer.phar dumpautoload.
If you want myname/commons/Acl to be a module, then you need to add a Module.php to myname/commons/Acl that looks like this:
<?php
namespace WebAcl;
class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
You now need to tell your application's ModuleManager to load this module in application.config.php:
Add 'WebAcl' to the list of 'modules'
Add the path to the module in the 'module_paths' key:
'module_paths' => array(
'WebAcl' => "./vendor/myname/commons/Acl",
'./module',
'./vendor',
),
You have to do this as there isn't a direct mapping from the namespace name (WebApi) to the path name on disk like there usually is in the ./modules directory.
The ModuleManager should now find your module and the autoloader should be able to autoload any file in the WebAcl namespace within vendor/myname/commons/Acl/src/WebApi.
Of course, the composer route is easier if you don't need any other features of a ZF2 Module.