Is it OK to wire routing-generale methods into the view? - symfony

many times I see, for example in Symfony:
html file:
<hr>
<?php $this->generateUrl('blog_show', array('slug' => 'slug-value')); ?>
<br>
this is a bad smell, like not using depencency injection: this way HTML is coupled to the current framework. I have to edit it if I want to move it to another framework.
Wouldnt it be better to just pass the generated url string from the controller?

You can separate your view from your backend as you want, but your frontend file has to refer to an url at some point, even if it is just an api.
Changing your url from <?php $this->generateUrl('blog_show', array('slug' => 'slug-value')); ?> to <?php $showUrl; ?> just make it less maintainable and harder to migrate to another framework.
If you want to be Framework Independant you should use Symfony backend like an API.

The url could be generated in the controller to separate model from view. But it's only a first step. The best way is to separate model, view and routing. I think the best way is to use a view helper which generates the path. I changed some framework for project (abandonware to symfony). It is easier when the project used a template (like Smarty or Twig)
Take a look at Symfony Twig extensions to have some example which separates path generation and controller.
As example, Twig purpose the path method. This view helper is searching the url path from the route paramter. It doesn't have any relation with controller or model.
path : Returns the relative URL (without the scheme and host) for the given
route. If relative is enabled, it'll create a path relative to the
current path.
As you can see there you only have to be carefull to the route name.
/**YourController**/
/**
* #Route("/foo", name="new-game", methods="get")
*
* #return Response
*/
public function myFooAction()
{
//Your code
//The rendering without information about his own or other path
return $this->render('default/foo.html.twig', []);
}
And the corresponding path in Twig :
<hr>
Some text
<br>

Related

Prestashop 1.7 override front controller from custom theme

In PrestaShop 1.7, I have my own theme, which is currently the default.
In the product card, I need to display a list of multishop stores.
Since I don't know if it is possible to get information about multishop stores using the default controller controllers/front/ProductController.php, I want to extend this controller in my custom theme.
I am creating file themes/myTheme/override/controllers/front/ProductController.php with code like this:
class ProductController extends ProductControllerCore{
public $multiStores;
public function init()
{
parent::init();
echo 'test';
}
}
I expect that on the product card page I will be able to see my text "test".
But unfortunately nothing happens, I conclude that this controller is not connected for some reason.
Can you please tell me what I'm doing wrong and how to fix the situation?
You cannot override a front controller method in a theme like that.
You'll need to override the core frontcontroller by placing
your code inside /override/controllers/front
See : https://devdocs.prestashop.com/1.7/modules/concepts/overrides/
Anyway you can achieve the desired result in a cleaner way by
using a proper frontend hook (ie. hookDisplayHeader) and build a simple module, checking the context object to see in which id_shop you are connected.

How to specify a function on a link

I am new to WordPress development. I am coming from Symfony and vanilla PHP development.
I want to assign a "route" or a function to generate an XML file for a specific post type.
I know that I can get the post information with WordPress functions like get_post_meta.
I don't know the how to assign a function to a link
DOWNLOAD XML.
What is the "WordPress way" to do this?
Wouldn't an Ajax call do the job?
I'm a new in WP also. So I did this way:
1) I made a template myxmltemplate.php in /wp-content/themes/mytheme/myxmltemplate.php starting with:
/**
* Template Name: XML
*/
2) I created a page with template XML with url for example example.com/xml/
3) I put all my code into myxmltemplate.php which generates XML code I need
4) after that I can make a link like this
DOWNLOAD XML
Maybe there is a better way to make routes with xml-generating (like in functions, but for me it's the simplest).

Symfony2 - Call the same getRepository in all controller actions

I'm using a repository in Symfony2-controller like this:
$blog = $em->getRepository('BlogBloggerBundle:BlogData')->getBlogData($id);
Before I call it into the twig view through an array.
All works right but the issue is that it make up a footer menu, then, i should call it almost in every action which i need.
How i can call it from a "common" repository every time which i need?
Create a view reponse listener registering a twig variable and add the repository call in there...
... or create a twig extension exposing the data received from the repository as a global twig variable.
... or (my preferred choice) create a controller dedicated to rendering the footer and include the footer like this:
{{ render(controller('Bundle:controller:action')) }}
Read more about rendering fragments in this blog post on the symfony homepage.
FYI Official documentation about embedding controllers : http://symfony.com/doc/current/book/templating.html#embedding-controllers

Output CCK items in custom template

I am working with a Drupal 6 site which relies heavily on CCK and views and I want to use the content in a webapp for iPhone. The webapp is built in HTML and CSS and will ultimately be converted to native using PhoneGap or similar and I want to import the formatted HTML using a JQuery ajax call. The plan is to create a view in the website which returns the data that I want for each page and then create a template for that view which outputs the data in the correct format for including.
I need to be very prescriptive with the HTML output which is where I am struggling. I'm not overly familiar with Drupal templating and what I have done seems like a lot of HTML is created when outputting a node, and it is all also contained within the site template. I need to be able to output exactly what I want e.g.
<div class="customclass">
<?php echo($cckfield) ?>
</div>
<?php foreach($cckarray as $key -> $value) {
echo(some html using $key and $value);
}?>
Hopefully the pseudocode above gives an indication of the level of control required. Is this possible using either core or with a module? Or do I need to roll my own pages and use the API rather than going for Views?
The problem with this is that there are many files collected together and merged into the output file. If you want clean HTML, you need to go through the different files that make up the final output (e.g. page.tpl, different modules files,...).
The best way to change the base HTML-output is to filter everything that you don't need out of the $node object that every site gets passed. It also holds all the CCK values in formated and unformated form as well as all other fields. SO creating a custom template with no HTML and the bare values of $node should give you what you want.

Drupal - different images for different languages

I have to "i18n" an existing drupal installation and add a second language.
I have an image on the homepage that is defined as a 'background-image' in a CSS file.
The image contains text, thus I need to show different images depending if the URL is:
http://example.com/en/
or
http://example.com/es/
How can I show a different image on the homepage depending on the user language (url based)?
I am pretty new to Drupal, so please don't assume I know anything!!!
update: I was hoping that I could define a particular image as "EN" and add an "ES" alternative, have one URL to the image (that can be used in templates or css etc), but depending on the user language, Drupal would serve up the language specific version. This would be ideal.
update2: Or maybe another approach is possible - to create a new 'content type' that simply contains an image so that: http://example.com/node/23 returns the pure image binary (image/jpeg) - then this node could be internationalized/translated like other nodes. I'm really struggling to find the 'Drupal' way to i18n images...
update3: Does Drupal store the user language in the session? If so, I could just write my own script, read the session language and serve a language specific image right? and completely avoid Drupal (eg: http://example.com/i18n-image.php?img=logo - ugly, but you get the idea), if so, how does Drupal store the session user language?
You can use the internationalization module: http://drupal.org/project/i18n
and use the function "i18n_get_lang()" to get the current language.
Name your files depending on the language:
image_LANGUAGE.jpg
image_fr.jpg
image_es.jpg
To embed the images:
<img src="<?php echo base_path().path_to_theme().'/images/'; ?>image_<?php echo i18n_get_lang(); ?>.jpg" />
EDIT:
To implement something similar for the CSS files:
You can create various CSS files depending on the language:
styles.css
styles_fr.css
styles_es.css
Into the main file (styles.css) you can put all CSS that is not related to a specific language.
Into the other files you can put the CSS related to a specific language.
styles_fr.css:
div.item1 { background: url('../images/item1_fr.jpg'); }
styles_es.css:
div.item1 { background: url('../images/item1_es.jpg'); }
And then you create a preprocess function for the page in your template.php file that will include the specific CSS style sheet based on the current language:
function YOURTHEME_preprocess_page(&$vars, $hook){
drupal_add_css(path_to_theme().'/css/styles.css');
drupal_add_css(path_to_theme().'/css/styles_'.i18n_get_lang().'.css');
}
Or you can directly use drupal_add_css() into a template file.
I would go with hook_preprocess_page. For example, it can be a good idea to switch style sheets based on the language:
function foobar_hook_preprocess_page(&$variables, $hook) {
global $language;
if($language != 'en') {
$sheets = array();
foreach($variables['css']['all']['theme'] as $stylesheet => $value) {
if (strstr($stylesheet, path_to_theme())) {
$stylesheet = str_replace('.css', '-'. $language->language .'.css', $stylesheet);
}
$sheets[$stylesheet] = $value;
}
$vars['css']['all']['theme'] = $sheets;
$vars['styles'] = drupal_get_css($vars['css']);
}
}
For further explanations, see this blogpost: http://becircle.com/language_based_stylesheet_switching_your_theme
Just to extend your answers for Drupal 7: There's a global variable $languagethat provides a standard class containing many information about the current language, including $language->language with the language short code.
It's defined in modules/locale/locale.admin.inc.
So call
<?php
global $language;
print 'images/'.$language->language.'/image.png';
?>
in your theme and catch all the "$language->language is undefined" errors.

Resources