I'm running a laravel project behind a reversed proxy which is why I need to force the root url and scheme:
URL::forceRootUrl($proxy_url);
URL::forceScheme($proxy_schema);
I've added this to the top of my /routes/web.php and it's working fine until I run:
php artisan optimize
It caches the routes in /bootstrap/cache without the forced url and scheme, so now all my urls are pointing to the wrong root url.
I've tried to move the code to /Providers/AppServiceProvider.php (both register and boot) in order to make it take effect when caching the routes but no luck.
I have to manually delete the routes cache file in /bootstrap/cache to make my routes work again.
Have do I make it take effect when caching the routes?
Edit:
I have also tried to create a global middleware where I do the force url and schema. Again it works fine before caching the routes, but when running php artisan optimize the routes are once again incorrect.
php artisan optimize removed since laravel 5.6 (source, source2)
Using URL::forceRootUrl and URL::forceScheme seems like a work-around for working with reverse proxies. The clean solution for it would be to add a trusted proxies in your configuration. This post explains the feature in full. But it comes down to:
Use the App\Http\Middleware\TrustProxies middleware
Edit the middleware $proxies property with the IP(s) of your load balancer
protected $proxies = [
'192.168.1.1',
'192.168.1.2',
];
Remove the following code from /routes/web.php
URL::forceRootUrl($proxy_url);
URL::forceScheme($proxy_schema);
Related
I use laravel framework on 2 websites,
The application backend is one, but view paths is different for each site.
There is nginx variable "WEBSITE", which is available in PHP (phpinfo prints it).
When I run application locally, all is right. But in production, I get exception what variable is not exists.
I discovered what during deploy running command php artisan config:cache, which cached all config in "bootstrap" directory. I don't may delete this command, because it ups performance of site. This command caches all config including app/config/view.php file that descript changes in view paths, like this
'paths' => [
resource_path('views/' . env('WEBSITE')),
],
but config already cached without variable and in this form is delivered to the production.
I don't understand how fix this problem, other that edit all views, base layouts for both sites. Does anyone know how to solve this problem in a simple way?
Just add a fallback by explicitly adding WEBSITE=somewebsiteurl in your production sites .env, then re-cache the config and try if it works.
I am currently working on an API only laravel application. In the controllers folder, there is an API folder that holds all controllers. The ForgotPasswordController is in the API folder as well.
When I run the command php artisan route:list I get the error below
Illuminate\Contracts\Container\BindingResolutionException : Target class [App\Http\Controllers\Auth\ForgotPasswordController] does not exist.
There is actually no ForgotPasswordController in the Auth folder. How do I handle this issue?
You have to make sure you are doing php artisan route:cache priority.
If the problem still persists, can you disable the auth provider and try?
For the sake of time considering that the project is live and I need to churn out a couple of features, I have moved both ForgotPassword and ResetPassword controllers back into the Auth folder. Ran a test to make sure nothing has been broken (everything works fine) and now I am able to list out the routes.
If you have Auth::routes() or Route::auth() in your routes file that would be generating routes to the ForgotPasswordController.
You would need to not be calling that or you would need to pass the proper option to it to have it not register those routes:
Auth::routes(['reset' => false]);
Depending on the version you are using this may not work. If that is the case you will have to not use this method at all and register the routes you want/need yourself.
I have used Laravel Boilerplate for development of my application. Beside that I have installed L5Modular with it. So i define the route Like following inside my Modules
<?php
Route::group(array('module' => 'test', 'middleware' => ['web','auth'], 'prefix'=>'frontend','namespace' => 'App\Modules\test\Controllers'), function() {
Route::resource('test', 'TestController');
});
But when i tried to access the route http://localhost/blog/public/test/create it's showing 404 Error.
Why my route not accessed? is there any error of defining route?
You need to configure a virtual host to get laravel working, that's quite easy and even easier if you're using any software such as MAMP or XAMPP.
If you're on a MAC I'd suggest to have a look at laravel valet: you'll have your web server running in seconds.
If you don't want to do any of this, you'll have to change a few things in order to get Laravel working in a sub directory.
You might forget to write frontend prefix to URL. Try to access by:
http://localhost/blog/public/frontend/test/create
I have a laravel project which I run from my local apache server directory.
The link for accessing this project is
www.localhost/project.dev/public/index.php
And I have a navigation menu
After I have set the APP_URL in .env file to
http://localhost/blog.dev/public/index.php/
I get no problems while navigating through the About and Contact pages in the project but when I access the Home page the browser goes to the
http://localhost/
but not to the
http://localhost/blog.dev/public/index.php/
How can I fix it? Here are my routes:
Route::get('/', 'PagesController#getIndex');
Route::get('about', 'PagesController#getAbout');
Route::get('contact', 'PagesController#getContact');
I think the best way is to setup a vhost in apache. if you want to work like this in your .env set the APP_URL= http://localhost/blog.dev/public/
Run php artisan serve in command line. The laravel buil-in server will active. And you will able to access your laravel application using correct url as like http://localhost:8000 try this and let me know if you have any problem.
Just starting with Laravel.
I've installed it on my wamp server and setup a virtual host in apache. The standard home controller works fine. I've installed the scaffold bundle and generated a blog according to the description at the bundle's github page. There was no errors.
I've added Route::controller(Controller::detect()); to my routes.php but none seem to be working. I've tried http://laravel.dev/blog/posts/create and http://laravel.dev/blog/post/create. The scaffold created 3 controllers, users.php, blog/posts.php and blog/comments.php.
laravel.dev is a virtual host pointing with D:\wamp\www\laravel\public as DocumentRoot.
What are the possible issues? Where do I start looking?
Dont use Route::controller(Controller::detect()) - its known to be buggy and causes alot of issue.
Just define each controller individually in the routes.
I suspect you might have to include "index.php" (http://laravel.dev/index.php/blog/posts/create) in your URLs for it to work. This is a default option set in Laravel.
If you would like to turn this off for cleaner URLs, set "index" to an empty string in config/application.php.
You can read more about this in the documentation: http://laravel.com/docs/install#cleaner-urls