I am trying to create a hook extension for Contao.
But Contao doesn't seem to be able to load my class from the namespace, which handles the hook.
This is my file structure:
I have tried changing names and added ".php" to the class, looked up tutorials, but I can't find what I am doing wrong. I am fairly inexperienced in this topic, so I might be missing something obvious.
autoload.php
ClassLoader::addNamespaces(array
(
'Memberlevels',
));
gister PSR-0 namespace
*/
if (class_exists('NamespaceClassLoader')) {
NamespaceClassLoader::add('Memberlevels', 'system/modules/memberlevels/classes');
}
if (class_exists('NamespaceClassLoader')) {
NamespaceClassLoader::addClassMap(array
(
'Memberlevels' => 'system/modules/memberlevels/classes/myClass.php'
));
}
/*
* Register the templates
*/
TemplateLoader::addFiles([
'cookiebar' => 'system/modules/memberlevels/templates',
]);
config.php
$GLOBALS['TL_HOOKS']['outputBackendTemplate'][] = array('Memberlevels\myClass', 'myOutputBackendTemplate');
I get the error message:
Attempted to load class "myClass" from namespace "Memberlevels". Did you forget a "use" statement for another namespace?
You are still using the old Contao 3 way of loading classes. In Contao 4, you should use the autoloading feature of composer. The default composer.json of the most recent Contao versions already include autoloading directives for the src/ folder of your Contao installation:
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
Using that, this is how you create & register a hook in a Contao 4.4 compatible way:
// src/EventListener/OutputBackendTemplateListener.php
namespace App\EventListener;
class OutputBackendTemplateListener
{
public function onOutputBackendTemplate(string $buffer, string $template): string
{
// Do something
return $buffer;
}
}
// app/Resources/contao/config/config.php
$GLOBALS['TL_HOOKS']['outputBackendTemplate'][] = [\App\EventListener\OutputBackendTemplateListener::class, 'onOutputBackendTemplate'];
Starting with Contao 4.8 you can also use annotations to register a hook, eliminating the need to register the hook in app/Resources/contao/config/config.php.
Related
I try to create a Custom Plugin for CakePHP 4. But everytime i become a error that the plugin cant find the classes of the plugin.
I load the plugin in the Application.php
use Wirecore\CakePHP_JWT\Plugin as CakePHPJwt;
$plugin = new CakePHPJwt();
$this->addPlugin($plugin);
In the composer.json file of cake its also loaded:
"autoload": {
"psr-4": {
"App\\": "src/",
"Wirecore\\CakePHP_JWT\\": "./plugins/CakePHP-JWT-Plugin/src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Test\\": "tests/",
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/",
"Wirecore\\CakePHP_JWT\\Test\\": "./plugins/CakePHP-JWT-Plugin/tests/"
}
}
and i create a file in /plugins/CakePHP-JWT-Plugin/src/Middleware/JwtMiddleware.php with example code of the documentation Middleware. I changed the namespace of the example code to
namespace Wirecore\CakePHP_JWT\Middleware;
after this i try to load the middle in the middleware function of the plugin but everytime i become this error:
Class 'Wirecore\CakePHP_JWT\Middleware\TrackingCookieMiddleware' not found
here the code of the middleware function:
use Wirecore\CakePHP_JWT\Middleware\TrackingCookieMiddleware;
$middlewareQueue->add(new TrackingCookieMiddleware());
return $middlewareQueue;
i have tried to change the namespace but it does not work, and i try it with composer dumpautoload but again it changed nothing. Any ideas ?
The autoload section in the composer file is what tells PHP how to find your namespaces on disk. Your namespace prefix for the class, Wirecore\CakePHP_JWT, is not the namespace you listed in the autoload section, you listed Wirecore\CakePHP-JWT-Plugin. Either fix the namespace declaration to match autoload, like so:
use Wirecore\CakePHP-JWT-Plugin\ .. etc
.. or change fix the autoload section to match the name listed in your namespace declaration:
"autoload": {
"psr-4": {
"Wirecore\\CakePHP_JWT\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Wirecore\\CakePHP_JWT\\Test\\": "tests/",
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
}
}
Also, I'm not positive what standards your following here, but they potentially don't look right.
Is this a standalone plugin? If so, I'd mirror the structure from another standalone plugin like https://github.com/dereuromark/cakephp-geo/
If this isn't a standalone plugin (which is what seems more probable), but just a custom plugin being put directly into an existing CakePHP app, you're classes should be stuck in /plugins not /src, per the docs https://book.cakephp.org/4/en/plugins.html#creating-your-own-plugins
You might want to just use the Bake utility as a starting point (that'll solve your autoload issues too!) and copy your code into the classes it creates instead.
So I've been trying to create a custom class file for all my custom classes and append them in there, I'm using namespaces in order to accomplish using them in my controller however the controller is not recognizing the class
Ok:
Here is my custom class file placed in my project as app/Library/robloxClasses.php
namespace App\Library;
class RobloxMaths{
public function sortArrayOfArray(&$array, $subfield)
{
$sortarray = array();
foreach ($array as $key => $row)
{
$sortarray[$key] = $row[$subfield];
}
array_multisort($sortarray, SORT_DESC, $array);
}
}
Library is a folder i made to store all my custom class files in there
Here is how my controller ApiController uses the file using it's namespace
<?php
namespace App\Http\Controllers;
use App\Library\RobloxMaths;
use Illuminate\Http\Request;
class ApiController extends Controller
{
protected $RobloxClass;
public function __construct(){
$this->$RobloxClass= new RobloxMaths;
}
}
?>
The problem is, it keeps returning:
Class 'App\Library\RobloxMaths' not found
And I honestly do not know what's wrong, please help!
NOTE: I have tried composer update, and it fixes it temporarily but after a day or two or after me installing new dependencies it notifies me that the Class 'App\Library\RobloxMaths' not found thing happened
help please
Ensure that you have correctly setup the psr-4 autoload root in your composer.json file and that your file/directory names match perfectly including capitalisation, then run composer dump-autoload.
Example:
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
This will assign the root namespace App to the app folder.
If you are using an IDE, there is a large chance that your use and namespace statements where auto-generated with the lowercase app to match the directory name. As all the names tie up correctly in the IDE there are no errors, but when it comes to running the PHP script the autoloader is not doing require|require_once on your files and therefore your classes are not available.
Option 1 (Do this)
Ensure that your namespaces have consistent capitalisation that matches the composer.json entry.
Option 2 (Written for completion)
Ensure that you add a new entry for this root namespace to your composer.json file. For the example above this would be:
"autoload": {
"psr-4": {
"App\\": "app/",
"app\\": "app/"
}
}
It's an old question, but I had the same problem with laravel 9,
The problem is that I've used php short tag <? instead of <?php at the custom class file.
it's silly but this was the solution for me.
I'm trying to figure out how to get composer to look for classes for a specific namespace under 2 directories. What I'm thinking of is this:
Default location: /src/MyModule/myClass.php
Override location: /config/override/MyModule/myClass.php
Now, I'd like to use composer to configure the autoloader to check if a class exists under the Override location. If it does, use this class. If not, load the class from the default location.
Is this possible using composer or would I have to implement this logic using my own autoloader?
From Composer documentation:
If you need to search for a same prefix in multiple directories, you
can specify them as an array as such:
{
"autoload": {
"psr-4": { "Monolog\\": ["src/", "lib/"] }
}
}
You would add that to your composer.json file of course. To do it programatically you can do this:
$autoloader = require __DIR__.'/../vendor/autoload.php';
$autoloader->addPsr4('MyModule\\', [ '/first/dir/MyModule', '/another/dir/MyModule' ]);
Maybe dumb question, I'm new to Symfony2 and I'm using it for one of my projects.
I'd like to be able to use a third party library, namely SSRSReport (an API for SSRS Reports).
I have put the library into Symfony/vendor/ssrs/lib/Ssrs/src.
There are many classes defined here, I don't need them to be autoloaded.
I simply don't know how to require and call them from a controller.
For sure this doesn't work
require_once '/vendor/ssrs/lib/Ssrs/src/SSRSReport.php';
class DefaultController extends Controller
{
public function viewAction()
{
define("UID", "xxxxxxxx");
define("PASWD", "xxxxxxxx");
define("SERVICE_URL", "http://xxx.xxx.xxx.xxx/ReportServer/");
$report = new SSRSReport(new Credentials(UID, PASWD), SERVICE_URL);
return $this->render('myBundle:Default:view.html.twig'
, array('report' => $report)
);
}
}
SSRSReport() and Credentials() used here, are 2 of many classes contained into the API.
First of all, I don't recommend putting non-symfony-managed libraries into /vendors. Since you're managing this library, put it into /src.
Secondly, when using classes that aren't namespace (i.e., are in the root namespace), make sure you reference them properly or else PHP will look in the current namespace (which, in this case, is your controller namespace)
Thirdly, the quick-and-dirty solution is to just properly include the files from the controller:
class DefaultController extends Controller
{
protected function includeSsrsSdk()
{
require_once(
$this->container->getParameter( 'kernel.root_dir' )
. '/../src/ssrs/lib/Ssrs/src/SSRSReport.php'
);
}
public function viewAction()
{
$this->includeSsrsSdk();
define("UID", "xxxxxxxx");
define("PASWD", "xxxxxxxx");
define("SERVICE_URL", "http://xxx.xxx.xxx.xxx/ReportServer/");
$report = new \SSRSReport(new \Credentials(UID, PASWD), SERVICE_URL);
return $this->render('myBundle:Default:view.html.twig'
, array('report' => $report)
);
}
}
But that locks your logic for including the library into this one controller. You could make a separate wrapper for the SDK that does this, or even register it as a service.
You are probably using composer with symfony, so this is my suggestion.
Instead of require_once, you should use composer's autoload mechanism for autoloading non namespaced libraries or functions http://getcomposer.org/doc/04-schema.md#files
So just update the autoload section in composer.json.
"autoload": {
"psr-0": { "": "src/" },
"files": ["src/SsrsReport/SSRSReport.php"]
},
For consuming the service I would either use a Facade (extends SSRSREport class) or a Factory which returns it.
I'm experimenting with creating an extension with the Silex php micro framework for user authentication but I can't seem to get the autoloader to work. Can anyone shed any light?
I have a directory structure like this (truncated)
usertest
|_lib
| |_silex.phar
| |_MyNamespace
| |_UserExtension.php
| |_User.php
|_www
|_index.php
The pertinent bits of index.php, which serves as the bootstrap and the front controller look like this:
require '../lib/silex.phar';
use Silex\Application;
use MyNamespace\UserExtension;
$app = new Application();
$app['autoloader']->registerNamespace( 'MyNamespace', '../lib' );
$app->register( new UserExtension() );
The class I'm trying to load looks similar this:
namespace MyNamespace;
use Silex\Application;
use Silex\ExtensionInterface;
class UserExtension implements ExtensionInterface {
public function register( Application $app ) {
$app['user'] = $app->share( function() use( $app ) {
return new User();
});
}
}
All pretty straight forward except it throws this error:
Fatal error: Class 'MyNamespace\UserExtension' not found in /home/meouw/Projects/php/usertest/www/index.php on line 8
I have dabbled with symfony2 and have successfully followed the instructions for setting up the universal class loader, but in this instance I am stumped. Am I missing something? Any help would be appreciated.
In recent versions of Silex the autoloader is deprecated and you should register all your namespaces through the composer.json file which imo is a nicer solution because you are centralizing your autoloading definitions.
Example:
{
"require": {
"silex/silex": "1.0.*#dev"
},
"autoload": {
"psr-0": {
"MyNameSpace": "src/"
}
}
}
In fact when you try to access the autoloader in any recent version of Silex the following RuntimeException is thrown:
You tried to access the autoloader service. The autoloader has been removed from Silex. It is recommended that you use Composer to manage your dependencies and handle your autoloading. See http://getcomposer.org for more information.
I'd use
$app['autoloader']->registerNamespace('MyNamespace', __DIR__.'/../lib');
Deprecated - As of 2014-10-21 PSR-0 has been marked as deprecated.
PSR-4 is now recommended as an alternative
That is why you should use PSR-4 syntax in composer.json
{
"require": {
"silex/silex": "1.0.*#dev",
},
"autoload": {
"psr-4": {
"Vendor\\Namespace\\": "/path"
}
}
}
To register namespaces, just call registerNamespaces() like this:
$app = new Silex\Application();
$app['autoloader']->registerNamespaces(array(
'Symfony' => __DIR__.'/../vendor/',
'Panda' => __DIR__.'/../vendor/SilexDiscountServiceProvider/src',
'Knp' => __DIR__.'/../vendor/KnpSilexExtensions/',
// ...
));
Both adding appropriate statement to the autoload section of composer.json and registering namespaces directly calling registerNamespace was not working for me, until I executed composer update in the projects folder.