Symfony5 path from Controller to a twig file inside a bundle - path

Hello I Have inside {root_dir}/src/veo/SomeBundle/Controller/SomeController.php
return $this->render('index.html.twig', array(
"article" => $articles
));
As default symfony includes {root_dir}/templates/
How can I override this to {root_dir}/src/veo/SomeBundle/Resource/views/

I resolved this by adding namespace inside config/packages/twig.yaml
twig:
default_path: '%kernel.project_dir%/templates'
paths:
"%kernel.project_dir%/src/Veo/SomeBundle/Resources/views": foo_bar
when#test:
twig:
strict_variables: true
and inside Controller:
return $this->render('#foo_bar/Anomalies/index.html.twig', array(

Related

Zend Framework 2 autoloading hybridauth Library

I want to autoload Hybridauth external library in my module name "Account", I have included all libraries in the location
/var/www/ZendSkeletonApplication/module/Account/lib/hybridauth/Hybrid
Also in my module.php , I have the following namespaces
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__.'/src/'.__NAMESPACE__,
'hybridauth' => __DIR__.'/lib/hybridauth/Hybrid',
),
),
And in /var/www/ZendSkeletonApplication/module/Account/lib/hybridauth/Hybrid Auth.php file I have put the namespace.
namespace hybridauth;
class Hybrid_Auth {
......
In my controller I have declared the namespace as
use hybridauth\Hybrid_Auth;
But now whenever I make a call to new Hybrid_Auth( $config );, it shows error unable to load class.
I don't know what I am doing wrong here or weather I am following the correct way to autoload external libs in ZF2.
Any suggestion or Help !

What is the base directory for Zend ViewModel setTemplate?

I'm trying to load a different view depending on the value taken from a Regex route, but I can't get the ViewModel to load the template
public function viewAction()
{
$page = $this->params()->fromRoute()["page"];
$view = new ViewModel();
$view->setTemplate('module/Base/view/base/index/$page');
return $view;
}
I'm following the conventional module setup:
Base
|- config
| |- module.config.php
|- src
| |- Base
| |- Controller
| |- IndexController.php
|- view
|- base
| |- index
| |- index.phtml
| |- other.phtml
|- layout
And my view_manager configuration is:
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
I've tried all sort of values in the path passed to setTemplate, but I just keep getting the application error message, but no error log or details (that may be because I haven't figured out displaying errors properly). I have confirmed that $page is getting the name of the template correctly from the URL, and the template is in the correct directory.
So, where does ViewModel start looking for templates?
I've tried:
$view->setTemplate('$page.phtml');
$view->setTemplate('$page');
$view->setTemplate('base/index/$page.phtml');
$view->setTemplate('base/index/$page');
$view->setTemplate('module/Base/view/base/index/$page.phtml');
$view->setTemplate('module/Base/view/base/index/$page');
Each module that requires view scripts should have a view_manager key in module.config.php
You have two choices when registering view scripts.
'view_manager' => [
'template_map' => [
'module-name/foo/bar' => __DIR__ . '/../view/foo/bar.phtml',
'custom_view_template_name' => __DIR__ . '/../view/some/other/path/bar.phtml',
],
'template_path_stack' => [
__DIR__ . '/../view',
],
],
Template Path Stack
This represents a collection (stack) of paths that the view resolver will try to find paths from.
The view scripts that you set on the view models in the controller would then be relative to this directory path. This option saves you time by 'automatically' resolving view script paths using a common naming convention.
For example, if you have FooModule and the file FooModule/view/foo-module/index/index.phtml the template path that should be used in the controller would be foo-module/index/index.
Template Map
A one to one mapping of template name to template path. This means the template path is exactly the same as the array key.
You should try to favor this method as using the template_path_stack incurs a slight performance hit as the view scripts need to be resolved at runtime.
I am using it like this:
after the view folder inside your module add the path. So I assume yours is
view->setTemplate('base/index/$page');
This should do the trick.

zend 2: Unable to render template ... resolver could not resolve to a file

I'm going through the zend 2 getting started tutorial and I hit a wall. I am at the point in the tutorial where my action controller loads a view via the indexAction():
public function indexAction() {
return new ViewModel(array(
//$albums inside index.phtml will contain data from this method
'albums' => $this->getAlbumTable()->fetchAll()
));
}
But when loading the page I see this error:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "album/album/index"; resolver could not resolve to a file
At this point I realized I don't know what the hell is happening. I don't even know where to begin troubleshooting this error. Before I scan all of the files for typos I'd really like to understand how this error can occur.
here is my modul.config.php:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' =>
'Album\Controller\AlbumController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'ablum' => __DIR__ . '/../view',
),
),
);
The error "Unable to render template "album/album/index" means that you have to add the index.phtml file under the /album/album directory under the 'Album' module's view directory. The index.phtml view template file is used for rendering the view for the index action of the AlbumController controller of the Album module. Because this file seems to be missing, the view template resolver couldn't find it.
In Zend Framework 2, you implement a view as a template file, which is a file
having .phtml extension ("phtml" stands for PHP+HTML). View templates have such
a name because they usually contain HTML code mixed with PHP code snippets used
for rendering the web pages. Views typically live inside of the view subdirectory of the module.
For beginner, I would recommend to read the Using Zend Framework 2 book. With this e-Book, you can save your time and efforts learning ZF2.
Also, the path is case sensitive i.e. it should be all be in lowercase album/index/index both folders' name and index.phtml else phpRenderer will not be able to trace the view file and render it.
You can also check if you have the following in your module.config.php
'view_manager' => [
'template_path_stack' => [
__DIR__ . '/../view',
],
],

Zend Framework Multi Language Integration Steps

I am new to zend framework2 and I am working on a site with multi language integration. Please give me the idea about, how the builtin library and translation file should be configured, and how its can be called from view file
ZF2 has already integrated I18n tools.
How to integrate it
module.config.php
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
Files *.mo
Following previous step, create a folder and add your en_US.mo (for example) using Poedit (simple and good application)
Module.php
public function onBootstrap($e)
{
$translator = $e->getApplication()->getServiceManager()->get('translator');
$translator
->setLocale(\Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']))
->setFallbackLocale('en_US');
}
rq: Personally I use a session to stock my locale but it depends if I need a SEO using language
// session container
$sessionContainer = new Container('locale');
// test if session language exists
if(!$sessionContainer->offsetExists('mylocale')){
// if not use the browser locale
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$sessionContainer->offsetSet('mylocale', Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']));
}else{
$sessionContainer->offsetSet('mylocale', 'en_US');
}
}
// translating system
$translator = $serviceManager->get('translator');
$translator ->setLocale($sessionContainer->mylocale)
->setFallbackLocale('en_US');
$mylocale = $sessionContainer->mylocale;
How to use it
In a view, just type that:
<?php echo $this->translate("Translate that!"); ?>
Some links to explore
http://samminds.com/2012/09/zend-framework-2-translate-i18n-locale/
http://framework.zend.com/manual/2.2/en/modules/zend.i18n.translating.html
http://framework.zend.com/manual/2.2/en/user-guide/styling-and-translations.html

Autoloading Zend Framework 2 packages with the StandardAutoloader

I was previously using a downloaded version of Zend Framework 2 and was basically able to do something like this:
// Set include paths (add Zend to the path)
set_include_path(get_include_path() . PATH_SEPARATOR . $__CONFIG['zendPath']);
// Setup the Zend Autoloader
require_once('library\Zend\Loader\StandardAutoloader.php');
$autoLoader = new StandardAutoloader(array(
'namespaces' => array(
'Zend' => $__CONFIG['zendPath'] . '/library/Zend'
)
));
This works because all Zend packages are inside the library folder. I now wanted to use composer to load only the Zend packages I need. The problem I'm running into is that the packages get arranged in the following way:
zendframework/zend-cache/Zend/...
zendframework/zend-loader/Zend/...
zendframework/zend-validator/Zend/...
etc.
I tried having separate namespace declarations in the StandardAutoloader like this:
$autoLoader = new StandardAutoloader(array(
'namespaces' => array(
'Zend\Cache' => $__CONFIG['zendPath'] . '/library/zend-cache/Zend/Cache',
'Zend\Loader' => $__CONFIG['zendPath'] . '/library/zend-loader/Zend/Loader',
'Zend\Validator' => $__CONFIG['zendPath'] . '/library/zend-validator/Zend/Validator'
)
));
That doesn't work. Am assuming the namespace probably can't have the backslashes in it? is there any way to make this work? Preferably without having to define each individual package.
You just have to fix the paths:
$libs = $__CONFIG['zendPath'];
$autoLoader = new StandardAutoloader(array(
'namespaces' => array(
'Zend\Cache' => $libs . '/library/zend-cache/Zend',
'Zend\Loader' => $libs . '/library/zend-loader/Zend',
'Zend\Validator' => $libs . '/library/zend-validator/Zend',
)
));
That should do the trick (it's un-tested, so I'm not sure about the trailing Zend
Let it autoload Zend for you:
$autoLoader = new StandardAutoloader(array(
'autoregister_zf' => true, // Auto load Zend Namespace..
'namespaces' => array(
// extra namespaces
)
));

Resources