Hello friendly people,
I am following a tutorial on Symfony, in which I try to display a flash message, but even though I followed the instructions, no flash message is shown.
Here is my function viewAction in my AdvertController:
public function viewAction($id)
{
return $this->render('NeoPlatformBundle:Advert:view.html.twig', array('id' => $id));
}
public function addAction(Request $request)
{
$session = $request->getSession();
$session->getFlashBag()->add('info' , 'This is a flash message');
$session->getFlashBag()->add('info', 'This is a second flash message');
return $this->redirectToRoute('neo_platform_view', array('id' => 5));
}
When I go to
http://localhost/Symfony/web/app_dev.php/platform/add
I only get this:
My view.html.twig is as follows:
{# src/Neo/PlatformBundle/Resources/view/Advert/view.html.twig #}
<!DOCTYPE html>
<html>
<head>
<title>Display of the announcement {{id}}</title>
</head>
<body>
<h1>Display of the announcement n°{{id}}!</h1>
<div>
{#We display all flash messages, whose name is "info" #}
{% for message in app.session.flashbag.get('info') %}
<p>Flash message : {{message}}</p>
{% endfor %}
</div>
</body>
</html>
Any clue?
Thanks in advance!
I'm following the example of the official website.
$enqueue->front([
'as' => 'vueJS',
'src' => Helper::assetUrl('/js/vue.js')
]);
And I need it to load in a view, I use the example of the official page.
<!DOCTYPE html>
<html>
<head>
<title>Profesores</title>
</head>
<body>
<h1>{{ title }}</h1>
<p>{{ content }}</p>
</body>
</html>
I use a controller to load the view
public function getProfesores()
{
$profesores = Profesor::all();
return view('#MyPlugin/profesores/lista.twig', [
'title' => 'My Demo',
'content' => 'Congrats on your demo view.',
'data'=> json_response($profesores->toArray())
]);
}
Maybe it's easy but it does not work for me, can someone help me?
I'm working with knp snappy bundle for generate reports from my app. But doesn't work, when I call my controller for generate the pdf throws the next error:
The exit status code '1' says something went wrong:
stderr: "Loading pages (1/6)
[> ] 0%
[======> ] 10%
[=======> ] 12%
Warning: Failed to load file:///gestionresiduospel/web/bundles/usuario/css/bootstrap.css (ignore)
Warning: Failed to load file:///gestionresiduospel/web/bundles/usuario/js/bootstrap.js (ignore)
[============================================================] 100%
Counting pages (2/6)
[============================================================] Object 1 of 1
Resolving links (4/6)
[============================================================] Object 1 of 1
Loading headers and footers (5/6)
Printing pages (6/6)
[> ] Preparing
[============================================================] Page 1 of 1
Done
Exit with code 1 due to network error: ContentNotFoundError
"
stdout: ""
command: "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" --lowquality "C:\Users\rodrigo\AppData\Local\Temp\knp_snappy5682878973ade7.09664058.html" "C:\Users\rodrigo\AppData\Local\Temp\knp_snappy56828789746968.46649162.pdf".
My controller is this:
public function historicosDetallePdfAction($id)
{
$em = $this->getDoctrine()->getManager();
//gets residuos peligrosos.
$solicitudRetiro= $em->getRepository('SolicitudRetiroBundle:SolicitudRetiro')->find($id);
//gets residuos no peligrosos
$residuosPRetirados = $em->getRepository('SolicitudRetiroBundle:RetiroResiduo')->findResiduos($id);
//retorna los residuos no peligrosos en la solicitud
$residuosNPRetirados = $em->getRepository('SolicitudRetiroBundle:RetiroResiduoNoPeligroso')->findResiduosNoPeligrosos($id);
// view generator
$html = $this->renderView('SolicitudRetiroBundle:Pdf:resumenSolicitudResiduoRetirado.html.twig', array(
'solicitudRetiro' => $solicitudRetiro,
'residuosRetirados' => $residuosPRetirados,
'residuosNoPel' => $residuosNPRetirados ));
// pdf generator
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="historico.pdf"'
)
);
}
I copied my url and run into console[CMD] with wkhtmltopdf.exe. In console my pdf is created but when I see the file only shows my login screen. So I think could be a issue about permission into my controller but I don't know how I pass this.
In other hand, if you know about another pdf generator(from html) that working into symfony this could be help me too.
I'm working under symfony2.7
This mean's that one of the resources in your HTML file can't be loaded by wkhtmltopdf, check in your HTML file if all your resources have an absolute URL like this : http://www.yourserver.com/images/myimage.jpg
If it's not the case try to set it :
{{ app.request.schemeAndHttpHost ~ 'resource relative url' }}
If you are loading assets with assetic in {% stylesheets %} or {% javascript %} or {% image %} you can also do
{{ app.request.schemeAndHttpHost ~ asset_url }}
I had two kind of problems here once:
The first one was because kpn snappy fails when trying to convert a *.js file. So I had to take *.js files in my case.
The second one was because of the path. I had to use the absolute filesystem path. By this, I mean, something like: /home/ubuntu/workspace/bootstrap/css/bootstrap.css this is used in href tag used in *html.twig file that is then transformed to pdf by snappy.
By doing this, I used some twig global variables.
twig:
form:
resources:
- 'MyBundle:Form:fields.html.twig'
- 'MyUploaderBundle:Form:fields.html.twig'
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
globals:
pathToWeb: "%kernel.root_dir%/../web"
bootstrapCss: "/home/ubuntu/workspace/MyApp/web/bootstrap/css/bootstrap.css"
mainCss: "/home/ubuntu/workspace/MyApp/web/css/main.css"
layoutCss: "/home/ubuntu/workspace/MyApp/web/css/layout.css"
In twig
<link rel="stylesheet" type="text/css" href="{{ bootstrapCss }}">
<link rel="stylesheet" type="text/css" href="{{ layoutCss }}">
<link rel="stylesheet" type="text/css" href="{{ mainCss }}">
You can use absolute_url() twig function{{ absolute_url(asset('assets/image.jpg')) }}
For me this worked, the error was from html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
{% for path in encore_entry_css_files('app') %}
<link rel="stylesheet" href="{{ absolute_url(path) }}">
{% endfor %}
</head>
This problem never gets old, I encountered this just now and I manage to solve it, here's how.
This problem mostly happens in Windows so this solution is for that.
1. Install this composer dependency wemersonjanuario/wkhtmltopdf-windows
composer require wemersonjanuario/wkhtmltopdf-windows "0.12.2.3"
2. Open your config/snappy.php and use this config.
<?php
return [
'pdf' => [
'enabled' => true,
'binary' => base_path('vendor\wemersonjanuario\wkhtmltopdf-windows\bin\64bit\wkhtmltopdf'),
'timeout' => false,
'options' => [],
'env' => [],
],
'image' => [
'enabled' => true,
'binary' => base_path('vendor\wemersonjanuario\wkhtmltoimage-windows\bin\64bit\wkhtmltoimage'),
'timeout' => false,
'options' => [
'enable-local-file-access' => true,
'keep-relative-links' => true,
],
'env' => [],
],
];
3. Make sure the blade file you loading for pdf does not contain external links, like the one below.
<head>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght#0,300;0,400;0,600;0,700;1,300;1,400;1,600;1,700&display=swap" rel="stylesheet">
</head>
4. Make sure you load assets using public_path()
<link href="{{ public_path('css/pdf.css') }}" rel="stylesheet">
<img src="{{ public_path('fancy-image.png') }}">
I'm trying to use the AliDatatableBundle and I follow every step as docs say, so this is how my controller looks like:
public function indexAction(Request $request) {
$this->_datatable();
return $this->render('PICommonBundle:Default:index.html.twig');
}
private function _datatable() {
return $this->get('datatable')
->setEntity("PIProyectoBundle:Proyectos", "p")
->setFields(
array(
"Nombre" => 'p.nombre',
"Centros" => 'c.descripcion',
"Unidades" => 'u.descripcion',
"_identifier_" => 'p.id')
)
->addJoin('p.centros', 'c', \Doctrine\ORM\Query\Expr\Join::INNER_JOIN)
->addJoin('p.unidades', 'u', \Doctrine\ORM\Query\Expr\Join::INNER_JOIN);
}
public function gridAction() {
return $this->_datatable()->execute();
}
And this is my template:
{% extends '::base.html.twig' %}
{% block content %}
<link href="{{ asset('bundles/alidatatable/css/demo_table.css') }}" type="text/css" rel="stylesheet" />
<link href="{{ asset('bundles/alidatatable/css/smoothness/jquery-ui-1.8.4.custom.css') }}" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="{{ asset('bundles/alidatatable/js/jquery.datatable.inc.js') }}"></script>
<script type="text/javascript" src="{{ asset('bundles/alidatatable/js/jquery.dataTables.min.js') }}"></script>
{{
datatable({
'edit_route' : 'editar-asistencia',
'delete_route' : 'eliminar-asistencia',
'js' : {
'sAjaxSource' : path('informes')
}
})
}}
{% endblock %}
But any time I try to execute the page I get this error:
DataTables warning (table id = 'ali-dta_98f13708210194c475687be6106a3b84'):
DataTables warning: JSON data from server could not be parsed. This is caused > by a JSON formatting error.
Any advice? I'm using latest jQuery 1.10.2.
are you spanish?
(i´m too !!)
I think your problem is whit sAjaxSource.
Read this tip:
Ali en github
At the end of post comment:
"I took a look on your code and even tested it: your error is too simple , the "sAjaxSource" have to include the route for the grid action and not the index action "
In my case this solved:
In (bundle) routing.yml:
empleados_grid:
pattern: /empleados_grid
defaults: { _controller: MyBundle:Empleados:grid }
and in twig template:
{{ datatable({
'edit_route' : 'empleados_update',
'delete_route' : 'empleados_delete',
'js' : {
'sAjaxSource' : path('empleados_grid')
}
})
}}
enter link description here
I am trying about this:
text
I have this in my twig
<a href='{{ path('likes_show_names') }}' rel='facebox'>
And than in controller:
$view= $this->renderView('WallBundle:Statuses:likes_names.html.twig');
return new Response($view);
No error appear the network (chrome) is displaying code get 200. Facebox open the pop up but the connntent.. is missing...
When i check response => preview its displaying: This request has no preview available
What i am doing wrong please?
I put Facebox in my web/ directory. The file structure looks like this:
web/facebox
web/js/jquery.js
Then, on the routing, I set my default and the ajax-called controller:
vendor_some_bundle_homepage:
pattern: /
defaults: { _controller: VendorSomeBundle:Default:index }
vendor_some_bundle_test:
pattern: /test
defaults: { _controller: VendorSomeBundle:Default:ajax }
Next, I created a simple controller for both routes:
<?php
namespace Vendor\SomeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
public function indexAction()
{
return $this->render('VendorSomeBundle:Default:indexTest.html.twig');
}
public function ajaxAction()
{
return $this->render('VendorSomeBundle:Default:ajaxTest.html.twig');
}
}
Then, and I think the most important file for you, the page where there is a link that open facebox :
<!-- indexTest.html.twig -->
<html>
<head>
<link href="{{ asset('facebox/src/facebox.css') }}" media="screen" rel="stylesheet" type="text/css"/>
</head>
<body>
<a href="{{ path('vendor_some_bundle_test') }}" rel='facebox'>click me</a>
<script src="{{ asset('js/jquery.js') }}" type="text/javascript"></script>
<script src="{{ asset('facebox/src/facebox.js') }}" type="text/javascript"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('a[rel*=facebox]').facebox();
});
</script>
</body>
</html>
Important: you should take care of your assets and of the routing. If there is some errors, they should be written in your app/log/dev.log file, or at last in your apache error.log.
Finally, create the view that will be included:
{# ajaxTest.html.twig #}
This is <em>some</em> remote content
This sample gave me the following result:
Note: there is still some assetic errors (see the close button at the right of the image), because I installed facebox quickly. The point of your question was the access to remote content, and here you have a sample you can follow to find your mistake.