On my local development I face the issue that my applicaton uses relative path, to the domain, e.g. /css/style.css.
This works generally good, as I can configure virutal hosts on my develop machine, e.g. localhost.foo, so that the relative path is resolved to localhost.foo/css/style.css.
In my current situation, I cannot edit the hosts file to setup a development domain, so that the relative path to the domain does not work anymore. My develop url looks like localhost/projectfoo/public.
So that the app works, it should reference to localhost/projectfoo/public/css/style.css. Based on the relative path in the code it now reference to locahost/css/style.css, and there it will - obviously - not find the requested files.
Is there a way to configure laravel, to use at one case localhost/projectfoo/public as URL and on the other case the standard?
You can always add a new entry in your config files, lets say in the config/app.php
// this is the one that comes with laravel
'url' => 'http://localhost',
// this is the one that you can define
'url_public' => 'http://localhost/something_else/public'
and then use it in your view like:
<link href="{{ config('app.url_public') }}/style/style.css" rel="stylesheet">
Laravel has a helper function asset which generates a full URL for a relative path, based on your config app.url value.
For example, if app.url is http://localhost:
In your view put {{ asset("css/style.css") }} and Laravel will convert this to http://localhost/css/style.css
So in your case all you need to do is change app.url to http://localhost/public and then start using that asset method in your views instead of the relative path.
Better yet, add a new environment variable to your .env file (make sure to put it in .env.example as well) called APP_URL, e.g.
APP_URL=http://localhost
Then in config/app.php set it to:
'url' => env('APP_URL'),
That way you only have to set it once per environment
Related
I create my web app with php follow mvc model.
When I set path css,img,js,... i use <link rel="stylesheet" href="./public/css/style.css">
in default controller I got css for my page. But when I call action like http://localhost/assignment/controller/action i lost my css. relative path become http://localhost/assignment/controller/action/public/css/style.css or http://localhost/assignment/controller/action/param/public/css/style.css.
Sounds like you might have a configuration or bundler issue. The quickest - not best fix - for developing would be to move your css file to where the public folder is located. You need to find the root folder where that link is calling from and link there.
If you are using a bundler or transpiler, you might need to look in the configs for what is being bundled.
Also make sure to follow this format for local files.
The right way of setting <a href=""> when it's a local file
Instead of using localhost to access the file, I've used domain name lsapp.com
When I try to link to css file using asset, it links to lsapp.com/css/app.css but doesn't work. Same index.php file in public folder opens while using lsapp.com.
I've tried many different methods but none worked.Please help.
Currently using this but doesn't work
<link href="{{ asset('css/app.css') }}" rel="stylesheet" type="text/css" />
But if I use localhost to open the file, css works fine.
http://localhost/lsapp/public/index.php
Laravel using in latest version Ithink that would be more conventional.
mix.styles([
'asset/css/app.css',
]);
Please refer :
https://laracasts.com/discuss/channels/laravel/best-way-to-use-custom-css-in-laravel
https://laravel.com/docs/5.7/mix
I would recommend to watch the free lesson from laracast over laravel-mix.
https://laracasts.com/series/learn-laravel-mix/episodes/1
You will learn a lot about compiling css & js.
Make sure you have your .env file set up correctly.
"i've set url in config/app.php as 'url' => env( 'http:// www. lsapp.com', http:// localhost')" is wrong.
env() takes a variable name from your .env file, not an URL. Revert those changes back to the default:
'url' => env('APP_URL', 'http://localhost'),
This basically says, "url is either the value of APP_URL from your environment (.env file) or, if that doesn't exist, use http://localhost"
In your .env file, set APP_URL to http://www.lsapp.com, run php artisan config:cache to load and cache the values from .env and you should be good to go.
I think it would be work.
Maybe you are assuming that you have your CSS folder inside your public directory, I think you need to require illuminate/html to your project.
I'm developing a Laravel project using wamp stack on windows. My project is located in a separate folder like C:\wamp64\www\[project name]. The annoying problem is with url paths in code. I want to handle them in a way that they work both locally and on production environment.
For example this an absolute link:
<a href="/posts/tags/{{ $tag }}">
It is intended to navigate user to [project name]/posts/... . In other words I want to get project root with a slash. If this is not possible, what is the correct way of handling paths then (on development and production environment). I'm a little confused with this. Please provide detailed information considering both WAMP and Laravel. And please give information about relative paths, too.
You can use url method url method will return base url
edit tag
url()
The url function generates a fully qualified URL to the given path
So what I finally did was to use the suggested method by #iCoders plus having APP_URL set in .env file to http://localhost/[project_name]/public.
This way url method resolves the correct path provided that your config/app.php contains 'url' => env('APP_URL', 'http://localhost') and you make the URL generator use APP_URL. For more information, check this link:
Laravel: Change base URL?
To differentiate development and production environments different .env files can be used for each.
I am running a local server on port 4567. I am trying to make it so that when my database seeds I save a reference to the home page on my site in my db. However I noticed when I run URL::to('/') in my seeds it only includes "localhost" without the port, but if I include it in my view code it comes out as "localhost:4567". Why is this? How can I fix it, if possibly, without writing if statement conditionals about what production environment I am in? Thank you.
Seed File result of URL::to('/')
http://localhost
View File result of URL::to('/')
http://localhost:4567
Either set APP_URL in env file
APP_URL=http://localhost:4567
Or set url in config/app.php
'url' => env('APP_URL', 'http://localhost:4567'),
Many internal functions and third-party libraries use the APP_URL .env var directly or via config('app.url'). The better way is to use the URL generator classes that Laravel provides, eg. the Url facade.
Even so, you'll see different results in Views versus in the CLI, or in Jobs (eg. links in emails). In the web context most of Laravel's URL generation is based on the server/request URL. For example, the url() helper calls methods in Illuminate\Routing\UrlGenerator that ultimately use Illuminate\Http\Request's URL methods.
The CLI and Queued Jobs don't have the Request object, so they have to fall back to something else. That's right, configuration.
So, even though links, redirects, and other generated URL's will function perfectly in the web, these can be "broken" (misconfigured ;) outside the HTTP request lifecycle if APP_URL isn't set in your .env, or directly in the app config as #Sachin Kumar points out.
When configuring one bundle in Symfony2, I needed to set up a static path to CSS file from web folder, i.e. a line from config.yml:
content_css: "%path_to_web%/bundles/mybundle/css/styles.css"
%kernel.root_dir% returns the absolute root server path, but what in this case is the way of getting a virtual path to web folder? Is there any special variable for that or do I need to hard code that path?
You need no extra variable. The web root is defined with your webserver config. That's the way you configure the content_css option.
If you can reach your app.php (or app_dev.php) simple via http://www.example.com/app.php, then all assets are simply with the path reachable
content_css: "/bundles/mybundle/css/styles.css"
If you have exposed the whole symfony directory (strictly not recommended) and your app.php is reachable under the http://www.example.com/web/app.php, then simply prefix the path with /web.
content_css: "/web/bundles/mybundle/css/styles.css"
EDIT: Or you use a parameter in the parameters.yml. If you read this and you store your source in git or other (strongly recommended), then you have a paramaters.yml.dist with defaults, and every system (every developer or production server) has his own parameters.yml. Then add a parameter to yours and to the prods (and also to the .dist with some default):
parameters:
# [...] some other parameters
my_web_root: "/myproject/web"
the option looks like
content_css: "%my_web_root%/bundles/mybundle/css/styles.css"
As the directory structure of Symfony is defined in the Standard Edition and not the Symfony2 framework, there is no special parameter to use.
%kernel.root_dir% is defined by using __DIR__ in the AppKernel class. That's the one that should be used as the base path, you can do something like: %kernel.root_dir%/../web/