Laravel 5 url() doesn't contain scheme - laravel

I'm quite new in Laravel.
I'm working on an existing Laravel 5.8 project. I installed it locally with HomeStead.
I noticed a strange behaviour on redirects: considering my homepage is http://project.test/, when there is a redirection, say to /redirected, Laravel sets the location to http://project.test/://project.test/redirected! As I tried to figure out wat was going on, I saw that the url('/') Laravel function gives me ://project.test instead of http://project.test.
request()->getSchemeAndHttpHost() gives http://project.test.
Example in web.php:
Route::get('/info', function (Request $request) {
echo url('/'); // gives '://project.test'
echo '<br>';
echo request()->getSchemeAndHttpHost(); // gives 'http://project.test'
// This will lead me to 'http://project.test/://project.test/redirect'
// return redirect('/redirect');
});
I have the same problem with the static resources.
Config:
'url' => env('APP_URL', 'http://project.test') in config/app.php
APP_URL=http://project.test in the .env file.
I certainly missed something, but so far I couldn't find any information about this.

when you redirect check if you wrote : "Redirect::home()" , try "return Redirect::home()" (note the return)
I tested your routes, and it's working nicelly for me.
I give you my config,just replace "tlara" wich is my project name with your's : "project"
in host.sam
127.0.0.1 tlara.test #laragon magic! (note is not my ip 192.xx.xx, 127.0.0.1 is universal localhost)
in .env
APP_URL=http://localhost
..
REDIS_HOST=127.0.0.1 (i don't know if it is revelant)
..
SESSION_DOMAIN='localhost'
clear
clear caches (routes, caches, view) (may be useless)
test
tlara.test/info
127.0.0.1/info
localhost/tlara/public/info

Related

Laravel vue 3 remove /build/ from url

Let me start of with the fact that im new to laravel and english is not my main language so dont mind my grammer. I have a project that has a laravel api with a vue front-end. The Laravel web routing redirects everything to a single blade file that contains the vue app. this way i can use the vue routing. This is has all been working fine for a while now but now im trying to build for production and ive run into the following issue.
after using npm run build to build for production laravel puts /build/ to every route im using through vue. This is very logical given that it uses the build folder in the public directory like it should. But its ofcourse verry ugly for the users. Is there a way to remove the /build/ from the url? (appart from redirecting /build/ to / in the .htacces file on the server)
You can set the environment variables in .env file for javascript using VITE_ prefix as below:
Please add the new environment variable in .env file as below:
VITE_BASE_URL="/"
Made the router related changes in your Vue file as below:
const router = createRouter({
history: createWebHistory(import.meta.env.VITE_BASE_URL || '/'),
routes: [
.....
],
})
Problem from import.meta.env.BASE_URL in vitejs, in build mode this import.meta.env.BASE_URL result "/build/" and development mode result, is "/"
I don't know where to change this, but I fix this problem with
const router = createRouter({
history: createWebHistory("/"),
routes: [
{
......
}
]
})
and fix it

Laravel - How to clear config:cache on each request

I am working on a project which will have 50 subdomains and I found a solution to load separate .env file based on a domain name and everything is working fine... now my problem is
I have to run command config:cache to clear the cache so sytem can load relevant .env file, otherwise, it keeps loading the older .env file. How can i ask system to do cache clear on each load in bootstrap/app.php file???
My Code to load .env files in bootstrap/app.php
$domain = '';
if(isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])){
$domain = $_SERVER['HTTP_HOST'];
}
if ($domain) {
$dotenv = \Dotenv\Dotenv::create(base_path(), '.env.'.$domain.'.env');
try {
$dotenv->overload();
} catch (\Dotenv\Exception\InvalidPathException $e) {
// No custom .env file found for this domain
}
}
I would advise against doing that because things WILL break.
However, to answer your question, you can use Artisan::call('config:clear') inside a middleware that you can call on each request.
But instead of doing that, you could build a middleware that detects the subdomain you're getting the request from and then call the command instead, just to avoid that extra load.
I used another way to solve this on my project. It is setting the config dynamically according to the request. The config is only valid for the current request. If the count of the dynamic config is less you can use
Config::set('myConfig.hostName', $hostName);
Before doing that you must use the package
use Illuminate\Support\Facades\Config;

Laravel admin route is not handel by laravel 6

my web.php file
Route::get('admin', function () {
return view('welcome');
});
it's not working
error im getting in blow page
it's showing apache2 error can anybody know what is wrong.
it should handel by laravel right .?
i m using
Laravel 6
mysql 5.7.29
Ubuntu 18.4
php artian serve gives me again diffrent error show in below
Thanks
The fact that it is showing 403 Forbidden is most probably because apache is trying to directory-list /admin folder but it is prohibited to do so.
I think in laravel directory public folder have the admin folder name. Please check.
If the admin folder exists rename folder name.
So the solution is to make sure that you do not have a folder 'admin' inside public folder.
you should visit /admin to view the page, remove public
You should add port 80 in your url
localhost:80/project_folder/public/admin
or you if you have xampp folder you can edit virtual host document root in vhost file pointed to your project folder
DocumentRoot "C:/xampp/htdocs/project/public"
so you can just use
localhost/admin

Lumen dot in URL

I am serving a file via Lumen route and URL contains dot and extension file. and when URL contains dot Lumen look for resources not executing routes.
I've tried to run it on production server and it works but on development environment it does not work.
This is my route:
$router->get('file/{file}', function (Request $request, $file) {
echo $file;
});
When I try to open this URL route will be run:
http://localhost:9001/file/photo.jpg
When I try to open this URL Lumen look for resources:
http://localhost:9001/file/photojpg

Unable to get laravel/database and restler working with versioning

I have an existing website up and running, and now I want to add a REST interface to it in an api subdirectory. I'm not able to get this to work with versioning. I installed like so (no errors):
$ php ~/bin/composer.phar create-project laravel/database --prefer-dist api
$ cd api
$ php ~/bin/composer.phar require restler/framework 3.0.0-RC6
Then I uncommented the lines in public/index.php related to Restler and add a new API class that just echos a string. If I run this via php artisan serve and look at it through the localhost URL, then the method works.
Now I want to enable versioning, so I added these lines to public/index.php
use Luracast\Restler\Defaults;
Defaults::$useUrlBasedVersioning = true;
And in app/controllers I created a v1 directory and moved Test.php into that. I also added a namespace directive to the file of the format namespace A\B\v1
When I restart the artisan server and query the API, I get a 404 error. I've tried as both http://localhost:8000/Test and http://localhost:8000/v1/Test
What have I forgotten to do?
Here is how I made it to work. Note the folder where I placed the api class file.
in index.php
use Luracast\Restler\Restler;
use Luracast\Restler\Defaults;
Defaults::$useUrlBasedVersioning = true;
$r = new Restler();
$r->addAPIClass('A\B\Test');
Test.php kept in app/controllers/A/B/v1/Test.php
<?php namespace A\B\v1;
class Test
{
public function get()
{
return 'working';
}
}
Both http://localhost:8000/v1/test and http://localhost:8000/test return "working"

Resources