Using absolute paths in Laravel with wamp - php

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.

Related

Larverl's response()->file() must be prefixed with the current directory

On a fresh Laravel 8 installation I follow these steps:
php artisan storage:link
Inside /public/storage/ I create folder images/ and inside I paste an image called picture.png
In web.php I define a route like this:
Route::get('/picture', function(
return response()->file(Storage::url("images/picture.png"));
))
However, if I visit this route in browser, the picture is not shown and the following error is thrown:
Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
The file "/storage/images/picture.png" does not exist
It works only if the url inside response()->file() is prefixed with ./ indicating the current directory like this:
return response()->file('./'.Storage::url("images/picture.png"));
I don't think it is a good idea to work with relative paths locally, the Storage and file methods should handle this. Maybe it is even an issue with the framework.
Any ideas how to go around this hack?
I think you didn't configure the local driver check the documentation https://laravel.com/docs/8.x/filesystem#the-public-disk and look for local driver.
If you configure it right it should return the "/" that you're adding manually.

Laravel project temporary access needs cPanel name

I need to show to my client the progress of a project that is already in the shared host.
In development, acessing just /localhost/ worked to me, but now that I've sent the files to the host (cPanel), it's breaking in some cases because I need to use the /~cpanelname in the IP. Is there a way to add this /~cpanelname to every URL? After the deployment and insertion of a domain, it will need to be removed also.
The problem basically is:
When I have an image, the src is:
[IP]/img/img.jpg
When it needs to be [IP]/~cpanelname/img/img.jpg
A link href that is /about needs to be /~cpanelname/about
Can you guys help me?
Thanks!
You should be using the asset(), route(), url() etc. helpers to construct all of your links and file references.
If you've done that correctly, you can then change the base URL for all URLs just by changing the config/app.php file's url parameter (typically by changing APP_URL in .env).

Change default URL or path for whole application?

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

Laravel: URL::to('/') port number for localhost not included in db seed files

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.

CakePHP Missing Controller - but it exists

I recently downloaded cakephp-1.3.4. I set it up on my web server. I followed the advanced installation settings. My folder structure is as follows.
/common/
cakephp/
app/
etc...
/htdoc/
The /htdoc folder is the webroot; cakephp resides in the common folder.
I have configured the paths in index.php to point to this folder structure. I have the app up and running. I created a layout, the app has picked it up (along with all the css and images - all that works).
I created a posts_controller.php in cakephp/app/controllers/. Now when I try to access the following page: http://localhost/posts. I get a message that the controller cannot be found and that I should create a app/controllers/posts_controller.php (it already exists!).
Also the strange thing is using the default pages_controller works. I created an about.ctp and dropped it in app/views/pages/about.ctp. Vising http://localhost/pages/about shows up as expected.
SOLUTION:
Sam helped me solve this problem (see the long comment thread below). The problem was I had set relative paths for my ROOT folder. This messed things up. The solution is to either directly set an absolute path or call realpath with your relative path for it to be resolved into the right absolute path.
Make sure your controller class is named correctly (should be PostsController) and inherits from AppController (not strictly necessary but good practice).

Categories