I'd like to use the asset function into my project where I'm only using Twig.
I tryed to use it but php return me this :
Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'Unknown "asset" function in "./Default/base.html.twig"
The solution is on sensiolabs.org.
I added this in conf.php:
//config/config.php
require_once('../vendor/autoload.php');
//twig
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('../app/views');
$twig = new Twig_Environment($loader);
$twig->addFunction(new \Twig_SimpleFunction('asset', function ($asset) {
// implement whatever logic you need to determine the asset path
return sprintf('../assets/%s', ltrim($asset, '/'));
}));
That's because Assetic itself is a standalone PHP library. So to use asset in twig you need to install the bundle.
Run the following composer command to install assetic bundle.
composer require symfony/assetic-bundle
Without installing the standalone package, twig won't able to find the asset function.
Related
I am trying to include a package from composer, but I am receiving a class not found error.
I have tried the below possibilities.
$supermeteor = new \Supermeteor\Supermeteor('XXXXXXXX');
and
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor('xxxxxxxx');
Packages composer.json:
"psr-4": {
"Supermeteor\\": ""
}
Packages namespace :
namespace Supermeteor;
Packages class name :
class Supermeteor() {}
Error message
Uncaught Error: Class 'Supermeteor\Supermeteor' not found in
C:\path\to\my\file.php:16
I just tested your package locally, and it seems to work fine for me using the same code as you provided in your question. This is how I tested it.
1. Create a new project
Create a new directory on your computer.
2. Add the package to a new project using Composer
Locate your new directory on the command line and add the package to your projects autoloader by running the below composer command.
composer require supermeteor/sdk-php
3. Use the package
Create an index.php file in the same directory as your composer.json and add the below code.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Supermeteor\Supermeteor;
$supermeteor = new Supermeteor("xxx");
4. Test the results
In the terminal window start a new php server to serve your project.
php -S localhost:8089
Now access the site via your browser at http://localhost:8089.
I'm trying to use Doctrine MongoDB ODM 2.0 beta on a project with the Yii2 framework, with composer version 1.8.4 and PHP 7.2, but I keep getting the error Fatal error: Uncaught Error: Call to a member function add() on boolean where the code runs $loader->add('Documents', __DIR__);
bootstrap.php file (in DIR/bootstrap.php):
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
$config = new Configuration();
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir(__DIR__ . '/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setDefaultDB('fsa');
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/Documents'));
$dm = DocumentManager::create(null, $config);
I already tried looking at How to properly Autoload Doctrine ODM annotations? and Laravel & Couchdb-ODM - The annotation "#Doctrine\ODM\CouchDB\Mapping\Annotations\Document" does not exist, or could not be auto-loaded and a host of other threads I can't quite recall for help, but I couldn't figure out a solution.
I also tried commenting out the lines below
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
and ran composer dump-autoload and on command line it returned Generated autoload files containing 544 classes, but then I got the problem
[Semantical Error] The annotation "#Doctrine\ODM\MongoDB\Mapping\Annotations\Document" in class Documents\Message does not exist, or could not be auto-loaded.
So the annotations are not auto-loading, and I have no idea how to fix that.
In the model I have:
<?php
namespace Documents;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use \Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
/** #ODM\Document */
class Message
{
/** #ODM\Id */
private $id;
/** #ODM\Field(type="int") */
private $sender_id;
...
I also posted a thread on github at https://github.com/doctrine/mongodb-odm/issues/1976. One commenter stated that "By default, the composer autoload file returns the autoloader in question, which seems to not be the case for you." How can I fix that? The only information I can find online is to put (inside composer.json) the lines:
"autoload": {
"psr-4": {
"Class\\": "src/"
}
},
but then what class should I be loading?
I'm very confused and being pretty new to all these tools (mongodb, yii2, etc.) doesn't help at all. I'm not sure what other information would be helpful else I would post it.
Thanks in advance.
So turns out that the problem (as was mentioned in https://github.com/doctrine/mongodb-odm/issues/1976) was that autoload.php was required twice - once in bootstrap.php and once in web/index.php (of the framework). After the require line in index.php was removed, everything worked fine.
I'm developing an application where I started using Kohana and now Koseven, and I need to use an api that was available in the composer, I followed the steps to download the files I created a folder to sell inside the application, and put in the bootstrap.php code to call the autoload of the composer.
But after doing this when trying to use a class of this api error of "class not found" occurs.
I do not know what else to do, can you help me?
In order to use composer with Kohana or Koseven, you need to call composer's autoloader from within bootstrap.php.
After Kohana::modules($modules); and before Route::set, insert the following code:
/**
* Autoload composer libraries
*
*/
require APPPATH . 'vendor/autoload.php';
This assumes your composer install command is run the from the root of your app, and it uses the default vendor directory.
Probably you must add this to your composer.json. (Check inc composer doc.) I don't know, because in modules directory I have second instance.
"extra": {
"installer-paths": {
"modules/{$name}/": ["type:kohana-module"]
}
},
And enable module composer as first:
Kohana::modules(array(
'composer' => MODPATH.'composer', //
'auth' => MODPATH.'auth', // Basic authentication
'cache' => MODPATH.'cache', // Caching with multiple backends
It works for me ko3
I installed a composer, downloaded the Aura, created an index.pxp and wrote in it:
require('vendor/autoload.php');
use Aura\Di\ContainerBuilder;
$builder = new ContainerBuilder();
$di = $builder->newInstance();
$object = $di->newInstance('Vendor\Package\ClassName');
But phpStorm says:Undefined namespase DI
And i have error: Fatal error: Class 'Aura\Di\ContainerBuilder' not found in... on line 4
I do as follows: http://auraphp.com/packages/3.x/Di/getting-started.html#1-1-1
In order for the composer auto-loader to pick up \Aura\Di, the dependency needs to be managed by composer.
You can easily do this by executing
composer require aura/di
which will add the dependency to your composer.json file and register with the auto-loader.
If you have manually downloaded and installed aura/di, you can revert that.
How to register namespaces (with PHP 5.3) in the Symfony 1.4 for the autoloader class feature (like the Symfony 2.0)?
You can use Autoloader from Symfony2 in Symfony 1.4 framework.
1. Copy Symfony2 classloaders to vendor directory of your Symfony 1.4 sandbox project:
SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/UniversalClassLoader.php
SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php
2. Modify your SF_ROOT_DIR/config/ProjectConfiguration.class.php file as follows:
require_once dirname(__FILE__) . '/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
require_once dirname(__FILE__) . '/../lib/autoload/sfClassLoader.class.php';
sfCoreAutoload::register();
class ProjectConfiguration extends sfProjectConfiguration {
public function setup() {
$this->namespacesClassLoader();
$this->enablePlugins('sfDoctrinePlugin');
}
public function namespacesClassLoader() {
if (extension_loaded('apc')) {
$loader = new ApcUniversalClassLoader('S2A');
} else {
$loader = new UniversalClassLoader();
}
$loader->registerNamespaces(array(
'Pohon' => __DIR__ . '/../lib/vendor/Pohon/src'));
$loader->register();
}
}
3. Register desired namespaces:
eg. I want to load class:
Pohon\Tools\String\Utils\Slugify.
Filename must be:
SF_ROOT_DIR/lib/vendor/Pohon/src/Pohon/Tools/String/Utils/Slugify.php
and registered namespace as follows:
Pohon => SF_ROOT_DIR/lib/vendor/Pohon/src
You can use Composer and it's very easy. Just install it on your machine (you probably have already since it's 2015 now) and run in your project folder:
composer init
You can then install all the packages you want with composer and include just this line in your ProjectConfiguration.class.php:
require_once __DIR__.'/../vendor/autoload.php';
Note that paths may differ if you changed the default Symfony1.4 directory structure.
Symfony uses the spl_autoload_register() function to register its own autoloader (sfAutoload).
You can register your own handler in the initialize() function of your Project/Application/Plugin. (whichever applies).
This is, for example, also what the Swift_Mailer plugin does: it registers its own autoloader when needed.