Identifier "twig" is not defined - Silex Framework - php

Thanks for you interest,
I have create this APP:
// web/index.php
require_once __DIR__.'/../vendor/autoload.php';
use Silex\Application;
use Silex\Provider\TwigServiceProvider;
$app = new Application();
$app['debug'] = true;
$app->register(new TwigServiceProvider(), array(
'twig.path' => __DIR__.'/templates',
'twig.class_path' => __DIR__.'/../vendor/twig/twig/lib',
'twig.options' => array('cache' => __DIR__.'/../cache'),
));
$app->get('/', function () use ($app) {
return $app['twig']->render('base.html.twig', array());
});
return $app;
But, when I load / in instance of base.html.twig be loaded, this error appear:
InvalidArgumentException: Identifier "twig" is not defined.
in /home/victor/workspace/testProject/vendor/pimple/pimple/lib/Pimple.php line 78
at Pimple->offsetGet('twig') in /home/victor/workspace/testProject/src/app.php line 41
at {closure}()
at call_user_func_array(object(Closure), array()) in /home/victor/workspace/testProject/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php line 145
at HttpKernel->handleRaw(object(Request), '1') in /home/victor/workspace/testProject/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php line 66
at HttpKernel->handle(object(Request), '1', true) in /home/victor/workspace/testProject/vendor/silex/silex/src/Silex/Application.php line 538
at Application->handle(object(Request)) in /home/victor/workspace/testProject/vendor/silex/silex/src/Silex/Application.php line 515
at Application->run() in /home/victor/workspace/testProject/web/index.php line 11
I have followed the official tutorial, installed it using composer and double checked all the paths...

Try removing the "twig.class_path" entry.
I have it like this, and it works:
$options = [
'twig.path' => ...,
'twig.options' => [
'strict_variables' => false
]
];
$this->_app->register(new TwigServiceProvider(), $options);

Related

Symfony2 application not working when moving vendor directory

I have installed OroCommerce, a Symfony2 based application. I want to have the 'vendor' directory on a higher level outside the users directory. I followed the steps at http://symfony.com/doc/current/configuration/override_dir_structure.html#override-the-vendor-directory but now the namespaces can't be found. I get the following error:
InvalidArgumentException in YamlFileLoader.php line 399: There is no extension able to load the configuration for "framework" (in /home/oro2/public_html/app/config/config_dev.yml). Looked for namespace "framework", found "web_profiler", "sensio_distribution", "debug"
in YamlFileLoader.php line 399
at YamlFileLoader->validate(array('imports' => array(array('resource' => 'config.yml')), 'framework' => array('router' => array('resource' => '%kernel.root_dir%/config/routing_dev.yml'), 'profiler' => array('only_exceptions' => false)), 'web_profiler' => array('toolbar' => true, 'intercept_redirects' => false), 'monolog' => array('handlers' => array('main' => array('type' => 'stream', 'path' => '%kernel.logs_dir%/%kernel.environment%.log', 'level' => 'debug'))), 'oro_assetic' => array('css_debug' => null, 'css_debug_all' => false), 'oro_message_queue' => array('client' => array('traceable_producer' => true))), '/home/oro2/public_html/app/config/config_dev.yml') in YamlFileLoader.php line 369
at YamlFileLoader->loadFile('/home/oro2/public_html/app/config/config_dev.yml') in YamlFileLoader.php line 44
at YamlFileLoader->load('/home/oro2/public_html/app/config/config_dev.yml', null) in DelegatingLoader.php line 45
at DelegatingLoader->load('/home/oro2/public_html/app/config/config_dev.yml') in AppKernel.php line 35
at AppKernel->registerContainerConfiguration(object(DelegatingLoader)) in bootstrap.php.cache line 2776
at Kernel->buildContainer() in bootstrap.php.cache line 2728
at Kernel->initializeContainer() in OroKernel.php line 290
at OroKernel->initializeContainer() in bootstrap.php.cache line 2507
at Kernel->boot() in OroKernel.php line 252
at OroKernel->boot() in bootstrap.php.cache line 2538
at Kernel->handle(object(Request)) in app_dev.php line 33
Am I forgetting something? Does anyone know how to solve this problem?
You are somewhere calling %kernel.root_dir%/config/routing_dev.yml which actually does not exist, because you have moved the config file dir

Facade error in laravel 5.2

I installed a package in Laravel 5.2 named jonnywilliamson/laragram
And put in config\app.php a alias and service provider like below :
'providers' => [
.
.
.,
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
Williamson\Laragram\Laravel\LaragramServiceProvider::class,
]
'aliases' => [
.
.
.,
'URL' => Illuminate\Support\Facades\URL::class,
'Validator' => Illuminate\Support\Facades\Validator::class,
'View' => Illuminate\Support\Facades\View::class,
'TG' => Williamson\Laragram\Laravel\LaragramFacade::class,
]
And in my contoller :
use TG;
public function test()
{
return TG::sendMsg('+989118000000', 'Hello there!');
}
And route:
Route::get('test', 'Services\Auth\Controller\v1_0\AuthController#test');
I also run the following commands :
composer dumpautoload
composer dumpautoload -o
php artisan cache:clear
php artisan clear-compiled
php artisan optimize
But still shows error like:
RuntimeException in Facade.php line 210:
A facade root has not been set.
in Facade.php line 210
at Facade::__callStatic('sendMsg', array('+989118000217', 'Hello there!')) in User.php line 68
at LaragramFacade::sendMsg('+989118000217', 'Hello there!') in User.php line 68
at AuthController->test('fa')
at call_user_func_array(array(object(AuthController), 'test'), array('lang' => 'fa')) in Controller.php line 80
at Controller->callAction('test', array('lang' => 'fa')) in ControllerDispatcher.php line 146
at ControllerDispatcher->call(object(AuthController), object(Route), 'test') in ControllerDispatcher.php line 94
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
How can i fix it?
The function you are trying to call is called sendMessage:
return TG::sendMessage('+989118000000', 'Hello there!');
You are using sendMsg, which is causing the error.

Laravel 5 - Storage Adapter Extending Will Not Work

For some reason I cannot extend the Storage Class like the example in the docs.
I have been trying to create an FTP adapter because it is required for the project I am working on.
Here is the Service Provider I have created
<?php namespace herbals\Providers;
use Storage;
use Illuminate\Filesystem\FilesystemServiceProvider as ServiceProvider;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp as FTPAdapter;
class FTPFileSystemServiceProvider extends ServiceProvider {
public function boot()
{
Storage::extend('ftp', function($app, $config)
{
$client = new FTPAdapter(array(
'host' => env('FTP_HOST') || '',
'username' => env('FTP_USERNAME') || '',
'password' => env('FTP_PASSWORD') || '',
'port' => env('FTP_PORT') || '',
'root' => env('FTP_ROOT') || '/',
'passive' => true,
'ssl' => true,
'timeout' => 30,
));
$filesystem = new Filesystem($client);
return $filesystem;
});
}
public function register()
{
//
}
}
Here is the error I keep getting
ErrorException in FilesystemManager.php line 232:
call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Filesystem\FilesystemAdapter' does not have a method 'createDriver'
in FilesystemManager.php line 232
at HandleExceptions->handleError('2', 'call_user_func_array() expects parameter 1 to be a valid callback, class 'Illuminate\Filesystem\FilesystemAdapter' does not have a method 'createDriver'', '/home/vagrant/Code/herbals/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php', '232', array('method' => 'createDriver', 'parameters' => array(null)))
at call_user_func_array(array(object(FilesystemAdapter), 'createDriver'), array(null)) in FilesystemManager.php line 232
at FilesystemManager->__call('createDriver', array(null)) in FilesystemManager.php line 97
at FilesystemManager->createDriver(null) in FilesystemManager.php line 97
at FilesystemManager->resolve('ftp') in FilesystemManager.php line 79
at FilesystemManager->get('ftp') in FilesystemManager.php line 68
at FilesystemManager->disk('ftp') in Facade.php line 210
at Facade::__callStatic('disk', array('ftp')) in routes.php line 26
at Storage::disk('ftp') in routes.php line 26
I think you are making a little mistake here, make sure you have the ftp drivers and its details (host, username,password ) in /config/filesystem.php and reference them in your provider using $config['host'] and so on...
After playing with the api for some time , I was able to figure it out , now follow these instructions : In you /config/filesystem.php , add the new driver settings like :
'ftp' => [
'driver' => 'ftp',
'host' =>'ftp-host',
'username' => 'ftp-username',
'password' => 'ftp-pass',
'port' => 21,
'ssl' => false,
],
Next we will create an Ftp Filemanager Driver Service Provider in /app/Providers ,save the file FtpFilesystemServiceProvider.php .. Below is how mine looks like
<?php
namespace App\Providers;
use Storage;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Ftp as FtpAdapter;
use Illuminate\Support\ServiceProvider as ServiceProvider;
class FtpFilesystemServiceProvider extends ServiceProvider {
//boot
public function boot()
{
Storage::extend('ftp',function($app, $config){
//init the client
$client = new FtpAdapter($config);
//lets now call the obj
$ftpFileSystem = new Filesystem($client);
//return
return $ftpFileSystem;
});
}//end boot
public function register()
{
//
}
}
After which we will edit /config/app.php and add the service provider to providers array so you will add the code below to the list of providers:
'App\Providers\FtpFilesystemServiceProvider',
We are done, you are off to go , yo can call the storage like this
Storage::disk("ftp");
or set ftp as your default driver , ....

how to include dbadapter in view/layout in zend framework 2.3?

i would use my database adapter in my app layout template, but i can't do it! this is the error:
Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'Zend\View\HelperPluginManager::get was unable to fetch or create an instance for getServiceLocator' in C:\Program Files (x86)\xampp\htdocs\Easyanimal\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:529 Stack trace: #0 C:\Program Files (x86)\xampp\htdocs\Easyanimal\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php(103): Zend\ServiceManager\ServiceManager->get('getServiceLocat...', true) #1 C:\Program Files (x86)\xampp\htdocs\Easyanimal\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php(377): Zend\ServiceManager\AbstractPluginManager->get('getServiceLocat...', NULL) #2 C:\Program Files (x86)\xampp\htdocs\Easyanimal\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php(396): Zend\View\Renderer\PhpRenderer->plugin('getServiceLocat...') #3 C:\Program Files (x86)\xampp\htdocs\Easyanimal\module\Application in C:\Program Files (x86)\xampp\htdocs\Easyanimal\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php on line 529
i tried to add directly in my application\view\layout like this:
$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
i tried to add within a Application\Module\dbAdapter like this:
namespace Application\Model;
class dbAdapter
{
protected $adapter;
public function getAdapter()
{
if (!$this->adapter) {
$sm = $this->getServiceLocator();
$this->adapter = $sm->get('Zend\Db\Adapter\Adapter');
}
return $this->adapter;
}
}
and I include it in application\view\layout
use Application\Model\dbAdapter;
$dbAdapter = new dbAdapter();
$dbAdapter-> getAdapter();
but the error is:
Fatal error: Call to undefined method Application\Model\dbAdapter::getServiceLocator() in C:\Program Files (x86)\xampp\htdocs\Easyanimal\module\Application\src\Application\Model\dbAdapter.php on line 10
this is the config\autoload\global.php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=zf2-testdb;host=localhost',
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
you should pass the adapter from the controller to the view as a parameter ($dbAdapter is your db-adapter instance).
in your controller:
$view = new ViewModel(array(
'dbAdapter' => $dbAdapter,
));
return $view;
or alternatively you can also just return an array:
return array(
'dbAdapter' => $dbAdapter,
);
in view (layout) you can access the variables like this:
<?php $dbAdapter ?>
although I don't know why you need the db-adapter in the template.
Try this one:
<?php $dbAdapter = Zend_Db_Table_Abstract::getDefaultAdapter(); ?>

Symfony2 on WAMP server is not working

I have installed WAMP server with following details
1. Apache httpd 2.2.22
2. PHP 5.4.3
3. MySQL 5.5.24
I want to run Symfony2 on this so I downloded this
Symfony_Standard_Vendors_2.2.1
And tried to make application from it
I am getting following error for it
1/1 InvalidArgumentException: There is no extension able to load the configuration for "framework" (in D:\Program files\wamp\www\Symfony\app/config/config_dev.yml). Looked for namespace "framework", found "security", "twig", "monolog", "swiftmailer", "assetic", "doctrine", "sensio_framework_extra", "jms_aop", "jms_di_extra", "jms_security_extra", "acme_demo", "web_profiler", "sensio_distribution"
in D:\Program files\wamp\www\Symfony\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Loader\YamlFileLoader.php line 268
at YamlFileLoader->validate(array('imports' => array(array('resource' => 'config.yml')), 'framework' => array('router' => array('resource' => '%kernel.root_dir%/config/routing_dev.yml'), 'profiler' => array('only_exceptions' => false)), 'web_profiler' => array('toolbar'
=> true, 'intercept_redirects' => false), 'monolog' => array('handlers' => array('main' => array('type' => 'stream', 'path'
=> '%kernel.logs_dir%/%kernel.environment%.log', 'level' => 'debug'), 'firephp' => array('type' => 'firephp', 'level' => 'info'), 'chromephp' => array('type' => 'chromephp', 'level' => 'info'))), 'assetic' => array('use_controller' => true)), 'D:\Program files\wamp\www\Symfony\app/config/config_dev.yml') in D:\Program files\wamp\www\Symfony\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Loader\YamlFileLoader.php line 238
at YamlFileLoader->loadFile('D:\Program files\wamp\www\Symfony\app/config/config_dev.yml') in D:\Program files\wamp\www\Symfony\vendor\symfony\symfony\src\Symfony\Component\DependencyInjection\Loader\YamlFileLoader.php line 42
at YamlFileLoader->load('D:\Program files\wamp\www\Symfony\app/config/config_dev.yml', null) in D:\Program files\wamp\www\Symfony\vendor\symfony\symfony\src\Symfony\Component\Config\Loader\DelegatingLoader.php line 52
at DelegatingLoader->load('D:\Program files\wamp\www\Symfony\app/config/config_dev.yml') in D:\Program files\wamp\www\Symfony\app\AppKernel.php line 36
at AppKernel->registerContainerConfiguration(object(DelegatingLoader)) in D:\Program files\wamp\www\Symfony\app\bootstrap.php.cache line 653
at Kernel->buildContainer() in D:\Program files\wamp\www\Symfony\app\bootstrap.php.cache line 593
at Kernel->initializeContainer() in D:\Program files\wamp\www\Symfony\app\bootstrap.php.cache line 378
at Kernel->boot() in D:\Program files\wamp\www\Symfony\app\bootstrap.php.cache line 409
at Kernel->handle(object(Request)) in D:\Program files\wamp\www\Symfony\web\app_dev.php line 26
Not able to understand, how solve this problem ?
or where is the issue ?
Have you ran check.php, config.php as nicely described in the README.md? I would also recommend to use Symfony with composer, instead of with vendors. Please check the documentation for a proper setup.
This kind of error is usually thrown if you forgot to register a bundle and have already put bundle-specific configurations into app/config/config.yml.
The 'framework' configuration namespace is provided by the FrameworkBundle.
Please make sure you have FrameworkBundle registered in your app/AppKernel.php
<?php
[...]
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
[...]
Please also clear your cache with app/console cache:clear ( probably not working aswell ) or remove the contents of app/cache folder manually.

Categories