Symfony autoloader component - php

I'm getting:
Fatal error: Class 'Twig_Loader_Filesystem'
<?php
require_once "library/Symfony/Component/ClassLoader/UniversalClassLoader.php";
use Symfony\Component\ClassLoader\UniversalClassLoader;
$loader = new UniversalClassLoader();
$loader->registerNamespace("Symfony\Component", "library/Symfony/Component");
$loader->registerPrefix("Twig_", "library/Twig");
$loader->register();
$loader = new Twig_Loader_Filesystem('templates');
$twig = new Twig_Environment($loader, array(
'cache' => '',
));
?>
The Twig folder is in library folder. Have I missunderstood on how to use the component?

Are you sure your path is correct?
Try to use __DIR__ . '/library'
Try to use DebugUniversalClassLoader to pin down the problem - you can catch a RuntimeException and see which file it actually tries to load.
EDIT:
Correct solution: If you try to load PEAR-style classes with prefix Twig_ from '/library/Twig', you should point it to '/library', because Twig_ prefix itself will be used as a directory name inside /library

Related

render template that is in the views directory but inside another directory

I'm having a hard time trying to make this little modification...
I want to render a file (login.twig) which is inside my views file but in another folder: /views/ajax_files/login.twig
do I have to do this everytime ?
require 'vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('views/ajax_files');
$twig = new Twig_Environment($loader, array(
'cache' => 'cache',
'auto_reload' => true,
));
echo $twig->render('login.twig');
Because on my index.php, that is already being declared
which I'm trying to write DRY code
require 'vendor/autoload.php';
$loader = new Twig_Loader_Filesystem('views');
$twig = new Twig_Environment($loader, array(
'cache' => 'cache',
'auto_reload' => true,
));
echo $twig->render('index.twig');
You can also define multiple template directories directly in your index.php with
$loader = new Twig_Loader_Filesystem(array($templateDir1, $templateDir2));
Please also have a look at http://twig.sensiolabs.org/doc/api.html#loaders, you could also use a loader chain or simply add the additional path to your existent filesystem loader, with some code like
$loader = $twig->getLoader();
$loader->addPath('another/path/to/templates'); //or $loader->prependPath('...')
$twig->setLoader($loader);

Symfony3 Hook Up Annotation Routes

I'm writing my own PHP framework built on top of Symfony components as a learning exercise. I followed the tutorial found at http://symfony.com/doc/current/create_framework/index.html to create my framework.
I'd now like to wire up my routes against my controllers using annotations. I currently have the following code to setup the routing:
// Create the route collection
$routes = new RouteCollection();
$routes->add('home', new Route('/{slug}', [
'slug' => '',
'_controller' => 'Controllers\HomeController::index',
]));
// Create a context using the current request
$context = new RequestContext();
$context->fromRequest($request);
// Create the url matcher
$matcher = new UrlMatcher($routes, $context);
// Try to get a matching route for the request
$request->attributes->add($matcher->match($request->getPathInfo()));
I have come across the following class to load the annotations but I'm not sure how to use it:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php
I'd appreciate it if someone could help.
Thanks
I've finally managed to get this working. First I changed where I included the autoload.php file to the following:
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__ . '/../vendor/autoload.php';
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
Then I changed the routes collection bit (in the question) to:
$reader = new AnnotationReader();
$locator = new FileLocator();
$annotationLoader = new AnnotatedRouteControllerLoader($reader);
$loader = new AnnotationDirectoryLoader($locator, $annotationLoader);
$routes = $loader->load(__DIR__ . '/../Controllers'); // Path to the app's controllers
Here's the code for the AnnotatedRouteControllerLoader:
class AnnotatedRouteControllerLoader extends AnnotationClassLoader {
protected function configureRoute(Route $route, ReflectionClass $class, ReflectionMethod $method, $annot) {
$route->setDefault('_controller', $class->getName() . '::' . $method->getName());
}
}
This has been taken from https://github.com/sensiolabs/SensioFrameworkExtraBundle/blob/master/Routing/AnnotatedRouteControllerLoader.php. You may wish to modify it to support additional annotations.
I hope this helps.

Working with PHP and Mysql code in TWIG

I am working with the TWIG framework for php, and would like to know how i would be able to include these php files inside my php code like i normally do.
<?php
session_start();
include("includes/db.php");
include("functions/searchfunctions.php");
include("functions/userSearchSession.php");
?>
The db file establishes the connection through mysqli to the database.
In your comments you mentioned you are using the Slim framework, which has an extension to support Twig templates.
Using the extension however, requires some additional setup though, you must install the extention, called Slim Views, and also the Twig core from within composer. Slim Views does not list Twig as a dependancy.
To get this working:
Use composer to get add both Slim Views And Twig
$ php composer require slim/views
$ php composer require twig/twig:~1.0
Configure your Slim Framework $app to use the new tempting engine.
$view = $app->view();
$view->parserOptions = array(
'debug' => true,
'cache' => dirname(__FILE__) . '/cache'
);
$view->parserExtensions = array(
new \Slim\Views\TwigExtension(),
);
At this point, Slim Framework is now using Twig when rendering pages. You can now do all of your includes and pass the variables to Twig:
<?php
// ./Slim_app.php
require 'vendor/autoload.php';
/*
* foo.php contains the following:
* <?php
* $foo = bar;
*
*/
require 'foo.php';
$app = new \Slim\Slim(array(
'view' => new \Slim\Views\Twig()
));
$view = $app->view();
$view->parserOptions = array(
'debug' => true,
'cache' => dirname(__FILE__) . '/cache'
);
$view->parserExtensions = array(
new \Slim\Views\TwigExtension(),
);
$app->get('/hello', function () use ($app, $foo) {
//twig_template.html.twig exists in the templates directory.
//(./templates/twig_template.html.twig)
$app->render('twig_template.html.twig', array('foo' => $foo));
});
$app->run();
?>
{# ./templates/twig_template.html.twig #}
{{ foo }}
Navigating to Slim_app.php/hello now displays the following:
More info on using Twig.

Fatal error HelperSet not found when setting up Doctrine Console (with CodeIgniter)

I've got a problem setting up Doctrine with CodeIgniter. When running the following error is coming up:
Fatal error: Class 'Symfony\Component\Console\Helper\HelperSet' not found in /Applications/MAMP/htdocs/CodeIgniter-2.2.1/application/doctrine.php on line 21
The folder structure looks like this
/application/
/application/doctrine.php
/application/libraries/
/application/libraries/Doctrine/
/application/libraries/Doctrine/Common
/application/libraries/Doctrine/DBAL
/application/libraries/Doctrine/ORM
/application/libraries/Doctrine/Symfony
/application/libraries/Doctrine/Doctrine.php
/application/libraries/Doctrine/index.html
This is the line 21
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
Cannot find out what is the problem..
Followed this tutorial: http://wildlyinaccurate.com/integrating-doctrine-2-with-codeigniter-2/
update This is my doctrine.php in the application folder
<?php
define('APPPATH', dirname(__FILE__) . '/');
define('BASEPATH', APPPATH . '/../system/');
define('ENVIRONMENT', 'development');
chdir(APPPATH);
require __DIR__ . '/libraries/Doctrine.php';
foreach ($GLOBALS as $helperSetCandidate) {
if ($helperSetCandidate instanceof \Symfony\Component\Console\Helper\HelperSet) {
$helperSet = $helperSetCandidate;
break;
}
}
$doctrine = new Doctrine;
$em = $doctrine->em;
$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
\Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet);
Just downloaded github version and without any extra settings everything seems to be ok.
Check if the HelperSet.php file exists in Doctrine/Symfony/Component/Console/Helper directory, and add these lines to Doctrine.php in libraries directory (you have to register Symfony classes):
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'your_path_to/Doctrine');
$symfonyClassLoader->register();
This GitHub repository is for CodeIgniter 2, don't use it.
Try this tutorial, it works fine for me, even for CI 3.

require_once missing doctrine zend framework

I'm integrating doctrine with Zend Framework. I've hit an error thrown from cli. It seems Zend_Application_Bootstrap_Bootstrap does not have a require_once for Zend_Application_Bootstrap_BootstrapAbstract. Has anyone hit this?
my cli-config.php
<?php
$classLoader = new \Doctrine\Common\ClassLoader('App', __DIR__ . "/../application/models");
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Cms', __DIR__ . "/../application/modules/cms-modules/models");
$classLoader->register();
$classLoader = new \Doctrine\Common\ClassLoader('Proxies', __DIR__ . "/../application/models");
$classLoader->register();
$config = new \Doctrine\ORM\Configuration();
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
$driverImpl = $config->newDefaultAnnotationDriver(array(
__DIR__."/../application/models/App",
__DIR__."/../application/modules/cms-modules/models/Cms"
));
$config->setMetadataDriverImpl($driverImpl);
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
// Database connection information
$connectionOptions = array(
'driver' => 'pdo_mysql',
'dbname' => 'bella',
'user' => 'username',
'password' => 'password',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
);
$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
$helperSet = new \Symfony\Component\Console\Helper\HelperSet( array(
'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));
Bootstrap class should extends the Bootstrap Abstract class.
class Bootstrap extends Zend_Application_Module_Bootstrap {
//.....
}
Zend_Application does not use require_once. It is one of the first packages in ZF 1.* that requires the Zend Autoloader.
Yep replacing the doctrine class loader with Zend's auto loader did the trick. I had to add the path to the namespaces directly to the php path using set_include_path. Is there a nicer way to do this? I see Doctrine's class loader allows you to specify both the path and namespace. Thanks for your help beberlei and Alex

Categories