Symfony assets on localhost (Wamp) - css

I am running symfony on my localhost for learn, i have configured my localhost folder http://localhost/symfony to http:/symfonyweb.
Everything works fine but style can't loading,
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
full url is: http://symfonyweb/assets/css/style.css

Your asset should be load using the function asset() which is linked to /web root directory.
In your case, your assets should be like <link rel="stylesheet" type="text/css" href="{{ asset('assets/css/style.css') }}">

Related

How to use mix function in laravel

I'm writing a program using laravel. To manage css and js, I used versioning and call it with a function like this
<link rel="stylesheet" href="{{ mix('/css/app.css') }}">
The code above produces html output as follows
<link rel="stylesheet" href="/css/app.css?id=53a226b301fc510ddf79">
When I upload to hosting, why does the above code only produce html like this ?
<link rel="stylesheet" href="/css/app.css"> (the mix-manifest.json / id file not load)
this is my localhost
and this is on the hosting
Did you run npm run dev after deploy it on your hosting?
Make sure in your webpack.mix.js have .version() method.
I think in your localhost your laravel run in development mode. And in the hosting the laravel run with production mode. Check your .env file.

Where can I add assets in symfony 5?

I'm using symfony 5 and I have no idea where can I pu my css, js, images files.
I read many posts about it but none helped me...
I tried:
composer require symfony/asset
symfony console assets:install
Here is the way i am using asset function in my twig files:
<link rel="stylesheet" type="text/css" href="{{ asset('css/base.css') }}">
And yes I have a file at public/css/base.css...
But when I inspect the result page, my line is replaced by:
<link rel="stylesheet" type="text/css" href="/css/base.css">
Did someone have any idea ?
Best regards,
If you're NOT using webpack (or webpack encore), your assets should be in your public folder which is the root of your public project. For example, if you have a file called style.css in public/css/style.css, you can access it in your browser with the url (assuming you use a local server started in the public dir) http://localhost:8000/css/style.css.
The Symfony asset component goal is to guess if your project is in a subdirectory and therefore to resolve the correct path. In order to use it in a Twig template with the previous imaginary file :
<link rel="stylesheet" href="{{ asset('css/style.css') }}">
The asset function will be able to produce the right path for your file.
If you're using webpack, the setup is a bit harder so if you don't I won't explain it here.

Webapp does not load images

I'm developing a webapp on Eclipse and this is the file system directory structure:
myapp
- src
- build
- WebContent
- pages
- css
- images
- modules
- META-INF
- WEB-INF
When I run this webapp with Tomcat (Run from Eclipse) I can load in the browser
the jsp pages contained in (myapp -> WebContent -> pages) using for example this URL:
http://hostname:8080/myapp/pages/somepage.jsp
However the problem is that the loading of the css and images in such somepage.jsp fail.
In the code of the somepage.jsp I pointed the css in this way:
<link href="../css/new_style.css" rel="stylesheet" type="text/css"/>
And the image in this way:
<img src="../images/someimage.png"/>
Problem is I have no clue why such images and css are not loaded.
The problem is in the relative path, always a safer approach to do something like
<link href="<%=request.getContextPath()%>/css/new_style.css" rel="stylesheet" type="text/css"/>
or a JSTL equivalent
<link href="${pageContext.request.contextPath}/css/new_style.css" rel="stylesheet" type="text/css"/>
than you're starting from the root, accounting for context path as well
Better way is to use ${pageContext.request.contextPath} as it returns the name of the application's context.
For ex,
myapp/css/new_style.css can be dyanmically formed as {pageContext.request.contextPath}myapp/css/new_style.css
Have a look here to know What does "./" (dot slash) refer to in terms of an HTML file path location?

Relative path in website with sub-directories

I want to develop project sites on my Mac's apache server and then upload to the ISP server when ready to launch. I run into trouble when the site has sub-directories. If my link statement uses a relative path:
<link rel="stylesheet" media="all" href="css/site.css" type="text/css"/>
It doesn't work for sub-directories which need: href="../css/site.css"
If I use absolute paths (which work on the ISP's server):
<link rel="stylesheet" media="all" href="/css/site.css" type="text/css"/>
it doesn't work for sub-directories on my apache server.
This is the same problem for images and javascript links. There must be a solution that allows developers to build "one" version of the site and move it to the ISP's server without changing all the path commands. I keep looking for an answer but so far I haven't found a solution.
This is a common issue in porting websites. I don't know if this is the best answer, but I use a server-side script to get the relative root and include using that as an absolute path.
<?php
$rootaccess = $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'];
include ($rootaccess.'/_sys/alpha.php');
?>
This is the include for a PHP header, but it might work for you like this:
<?php
$rootaccess = $_SERVER['SUBDOMAIN_DOCUMENT_ROOT'];
?>
<link rel="stylesheet" type="text/css" href="<?php
echo $rootaccess;
?>/dir1/file.ext" />

Symfony 2 - having troubles with asset function

I'm having troubles with calling stylesheets and javascript via asset() function..
my app is running on dev enviorment so the url is - dproc.local/app_dev.php/
The base.css is in /src/Dproc/Resources/views/Dproc/css/base.css
<link href="{{ asset('/css/base.css') }}" type="text/css" rel="stylesheet" />
Doesnt work.
How should my link have to be? I didnt understood how asset() function works..
Thanks
You need to run the command
asset install --web
so that the asset files are copied to web folder.

Resources