Image doesn't show when using Silex and Twig - symfony

I use Silex-Skeleton. Twig layout is present in templates directory (templates/layout.html). I store images in web/img directory (top.jpg). I also use built-in php5.4 server running:
php -S localhost:8000 web/index_dev.php
In templates/layout.html I'd like to use web/img/top.jpg picture. So I put there:
<img src="{{ app.request.basepath }}/img/top.jpg">
But image doesn't show.
I've also tried:
<img src="/img/top.jpg">
<img src="/web/img/top.jpg">
But with no success either.
My controller looks like (index.html extends layout.html):
$app->get('/', function () use ($app) {
return $app['twig']->render('index.html', array());
})->bind('homepage');
What should I do to get images to work?

Can you navigate to http://localhost:8000/web/img/top.jpg or http://localhost:8000/img/top.jpg directly? If you can't browse and see them directly they will not render as the browser will not be able to find them to display on your page either.
Are your images actually in or below the /web folder? (i.e. they should not be in your /templates folder)
Perhaps you could try to set the docroot explicitly sing the -t option when you launch the PHP webserver.
Otherwise I would suggest setting up a real web server like Apache. Your OS may already have this installed if you are running OS X or *nix.

Related

CSS file is not found on localhost

I have a nodeJs app and I am using expressJs framework, I am trying to run:
app.use('/public', express.static(path.join(__dirname,'public')));
But I am getting this error:
CSS file is not found on localhost
app.use('/public', express.static(path.join(__dirname,'public')));
This is creating a virtual path to itself. You should use this approach when you want to name it something else. For example, let's say you want to use /assets in the path, you'd state: app.use('/assets', express.static(path.join(__dirname,'public')));
But since you have the an actual folder named public, you can just write:
app.use(express.static('public'))

KnpSnappyBundle and Symfony 3.4 : images and/or css cause timeout

I've installed KnpSnappyBundle on an existing Symfony 3.4 project. I've tested the PDF generator with a HTML twig with text only, no images, no css, no js : it works fine.
Then I've added (to the twig) an image and a Bootstrap.Css file using absolute URL (i'm working on localhost): the PDF generator displays an ugly error :
The process "wkhtmltopdf --lowquality '/tmp/knp_snappy5aeb39ad71e767.56551505.html' '/tmp/knp_snappy5aeb39ad71ebf0.62787578.pdf'" exceeded the timeout of 60 seconds.
What's wrong with the bundle ? I'm using an i7 laptop with 8Gb RAM (ubuntu 16.04), I don't think it's a problem of machine configuration.
UPDATE: I've tested wkhtmltopdf from the command line and it do convert my twig :
wkhtmltopdf http://127.0.0.1:8000/eshop/admin/order/print/2 out.pdf
So wkhtmltopdf have no problems with absolute URLs on localhost !
Thanks
Found a solution here : https://github.com/KnpLabs/KnpSnappyBundle/issues/82
It seems like wk is unable to find assets using the absolute URL when called from the bundle (so when executed in shell), you have to specify an absolute path. Create a twig global variable :
twig:
globals:
pathToWeb: "%kernel.root_dir%/../web"
Then in the twig use the new variable instead of asset() :
<link ... href="{{ pathToWeb }}/css/bootstrap.min.css"/>
<img ... src="{{ pathToWeb }}/images/logo.png"/>
Works like a charm

Wordpress - Get root directory?

How can I get the root directory of my site? I want to use it as the src for <img>.
For example If I install the wordpress on www.mysite.com/blog/, the root is /blog.
But If I install it to www.mysite.com/test/blog, the root is /test/blog.
I tried to use the snippet from this question, but it returns the whole path as seen in FTP. So instead of returning just /blog, it returns /home/public_html/blog which won't work if used as src in image tag.
Any solution? Thanks.
You may use site_url() (eventually with echo) to get absolute path with server name and directory. Also, have a look at wordpress documentation about similar functions & what they provide.
You may have better luck over at the Wordpress Stack Exchange site :)
And this suggestion to use ABSPATH didn't help on that thread? https://stackoverflow.com/a/2356467/413254
site_url() return the path with http, in some cases, it is not allowed if the server turns off url inclusion. You can use the function below to get the root path of wordpress installation folder:
function get_wp_installation()
{
$full_path = getcwd();
$ar = explode("wp-", $full_path);
return $ar[0];
}

Problems with image path

I have a project built in CodeIgniter. In my localhost, the website work fine, but in the remote server, fail the image inclusion from CSS & JS.
The site, is guest in a subdomain. The server file system hierarchy is:
--/
---apps (subdomain folder for my app)
----application
----[other CI forlders]
----css
----js
----img
--[other web folders (no CI)]
--index.html
In CSS files i define the images paths this way: "../img/"
For example:
#maincontainer {background-image: url('../img/main_bg.gif');}
But, in Chrome and FF, show error to load resources:
Failed to load resource: the server responded with a status of 404
(Not Found) http://apps.manantiales.edu.ar/img/main_bg.gif
Any ideas?
You are on a subdomain, therefore, the correct path would be:
http://manantiales.edu.ar/apps/img/main_bg.gif
You can use rules in your htaccess file to make the subdomain path work
or
Just use absolute paths like diEcho suggested. You can embed php code in css files like this:
maincontainer {background-image: url('< ?php echo site_url('img/main_bg.gif');?>');}
(I have put an extraspace at < ?php as not sure how to format it to show correctly here)
You can simply do it like this
--/
---apps (subdomain folder for my app)
----application
----[other CI forlders]
--[other web folders (no CI)]
--index.html
--css
--js
--img
#maincontainer {background-image: url('../img/main_bg.gif');}
and in html files;
<?= base_url();?>css/css.css

convert .WAR for auto deploy in karaf/servicemix

I've got very simple .WAR containing example servlet. I'm able to deploy it in servicemix using the following command:
osgi:install file:///home/seiho/apache-servicemix-4.4.2/deploy/TestServlet.war?Bundle-SymbolicName=TestServlet&Webapp-Context=/TestServlet
And then see it in my browser. But only with full path to a file, e.g.: localhost:8080/TestServlet/index.html or localhost:8080/TestServlet/TestServlet (my servlet is TestServlet class).
I'd like to launch the index.html page automatically after entering: localhost:8080/TestServlet
how to do it?
MORE IMPORTANT
I need a way to convert the .WAR file or servlet project (I've got the sources) so that new .WAR file can be auto-deployed by copying it to $SERVICEMIX_HOME/deploy directory.
I've tried editing the MANIFEST.MF file, but with no success. Probably I'm doing something wrong.
Thanks for any advice/help.
To be recognised as a wab, you need to add a context path header to your manifest:
Web-ContextPath: TestServlet
It's working now! I was doing my MANIFEST.MF according to this page: http://team.ops4j.org/wiki/display/ops4j/Pax+Web+Extender+-+War+-+OSGi-fy
The problem was that for some reason "Bundle-Version: 1.0" line was required as opposed to optional as stated on that page.
Honestly, just adding the Bundle-Version fix-it.
I knew it was something wrong with the MANIFEST.MF and after Holly Cummins' question I played with it a bit more. Thanks Holly.
I still can't do anything with the manual site launching (have to manually enter the index.html).
http://localhost:8080/TestServlet/ gives me this:
HTTP ERROR 404
Problem accessing /TestServlet/. Reason:
Not Found
Powered by Jetty://
http://localhost:8080/TestServlet/index.html gives me proper site.

Resources