How can I force artisan to serve with local environment? - laravel

I tried
php artisan serve --env=local
But its still serving the development site.
Laravel development server started on http://localhost:8000
I know I can edit the app config, but for the project I'm working on I need to switch between the two modes often and thats going to be a pain.
ps. I'm using 4.2

It's a bit hacky, but I got it to work like this.
Put at the very top of artisan
if(in_array('serve', $argv))
if(in_array('--env=local', $argv))
file_put_contents('.isLocal','');
else if(file_exists('.isLocal'))
unlink('.isLocal');
And replace the existing environment detection logic with this
$env = $app->detectEnvironment(array(
'local' => file_exists('.isLocal'),
));
Basically artisan will now create an .isLocal file whenever the --env=local option is set and delete it when its not. Make sure to add .isLocal to your .gitignore

Related

From Local to Live, which files to edit - Laravel

strangely I have never programmed locally, I am a beginner but unfortunately I have always created my little scripts directly live. I am using Laravel with Xampp, but now I would like to put my script online.
My question is, what files do I need to edit to make it effective online? At the moment I edit the .env file locally to connect to the database, but is it the same even if I put it online?
Thanks
Here you can find some basic information: https://laravel.com/docs/7.x/installation and https://laravel.com/docs/7.x/deployment
In general, to have a site in production (Laravel or not) it is better to manage your code with git (through Github, Gilab, etc.)
to have a clean version management (the alternative is to use Ftp trought Cpanel, ...).
If you are a beginner, you can go to https://forge.laravel.com/ which is a portal that greatly facilitates the production of a Laravel project.
Otherwise, a generic hosting is better that has the possibility to connect via ssh, in order to easily execute the commands
(composer install, php artisan, ...).
You can do many optimizations in production (I leave out the ones on the .env file you know):
When you do maintenance on the site it is always best to disable it first(status 503):
php artisan down
Upload the latest version of the code from the git repository:
git pull
Clean the item cache on the server:
php artisan cache:clear
Clean the route cache and recreate it (to do if there is no static code in web.php, but only references to code in the controller):
php artisan route:clear
php artisan route:cache
Clean and optimize the config file (reduces the number of files to be read from ten to one):
php artisan config:clear
php artisan config:cache
Clean up expired passwords and reset tokens (clean up tokens when a user requests a password reset):
php artisan auth:clear-resets
Recreate the framework classes or update the application:
composer dump-autoload or composer install
Activate the site:
php artisan up

Laravel Serve command does not respect --env parameter

To be able to run Browser tests directly in my IDE (without using the artisan dusk command), I want to run php artisan serve --env=dusk.local. While it indeed starts the local PHP server, it uses the wrong database. It uses the database specified in .env not the one in .env.dusk.local.
I ran php artisan cache:clear thousands of times, but it doesn't change anything.
Running things like php artisan migrate --env=... works.
Is there a way to achieve my goal without needing to rename my .env.dusk.local file to .env before each test?
This is a bug in Laravel 5.8: https://github.com/laravel/framework/issues/27828
There is currently no solution (other than downgrading to Laravel 5.7).
It has been fixed in the latest release 5.8.7.

Using NGINX on Kubernetes tries to load the assets with private IP rather than the domain

So i'm using Laravel with Kubernetes and everything works great, except for the fact that when i access the website, it takes too much to load. I troubleshot it and i found out that some CSS and JS files are loaded using the private ip (the one that starts with 10: 10.244.xx.xx)
I have no idea what's going on. Is it some kind of NGINX setting that messes it up? I am using the default NGINX Ingress for the cluster and i repeat: everything works great, except with this particular thing.
Edit: It seems like the route:cache command messes everything. I don't know why.
Never use secure_asset() over asset() unless you know what it can do.
I had to replace all my secure_asset() with asset()
make sure you got the domain right in .env file, in the root folder of your Application run:
sudo nano .env
find APP_URL parameter and configure it right, then run:
php artisan config:cache
So, i found it. It seems like i had to run route:cache between config:cache and view:cache on each pod deployment.

Laravel-Php artisan serve url (127.0.0.1:8000) vs localhost/laravelproject/public

I want to access my laravel project.I run php artisan serve and access the 127.0.0.1:8000 in browser.
But i learned that I can also check my project using the localhost/laravelproject/public url whithout running php artisan serve.
Question: What is the point of using php artisan serve?
No point in two different methods like you mentioned run laravel by "php artisan serve" and by "project url" followed by localhost. But advantage of "php artisan serve" is you can run you laravel project without putting in htdocs/www directory i.e servers root directory. You can put laravel project anywhere where you want and run through the artisan command.
I found some information you may find interesting:
https://www.quora.com/How-can-I-use-php-artisan-serve
But in simple words, php artisan serve is an easy way to create a php server and that is what laravel needs to run.
You could do the same with "php -S 8080 (which would start a php web server (single threaded) in the current directory on port 8080)"
Also if you have already a php server running with apache or nginx it would not be necessary any of the commands.
Hope you find this helpful.
The `Serve command is just a shortcut for the PHP Builtin Webserver, something PHP has out of the box, so the point of using it is to start testing your application as fast as you could, you just need to install PHP, Composer and your application is up (if you don't need anything else, of course). But if you already have Nginx installed, there is no point at all, just use it.
It's not wise to use the Builtin Webserver in production.

Lumen not working out of the box

Just installed Lumen framework.
hit the link http://localhost/lumen/public/ in my browser and got this following error, anyone got any idea about it?
Traced it back to the app.php file in bootstrap folder.
if You want to access lumen project without "php artisan serve"
$app->run(); replace with
$request = Illuminate\Http\Request::capture();
$app->run($request);
from this path yourlumenproject/public/index.php
Open your terminal in the root folder run the following command php artisan serve.
Lumen development server started on http://localhost:8000/
if you want to serve your app in local development you can do this :
php -S localhost:8000 -t public/
and it will serve in localhost in port 8000. hope this help.
Note : I'm using Laravel Framework version Lumen (5.2.4) (Laravel Components 5.2.*)
At the moment Lumen only runs in the domain root.
(I've submitted a PR that fixes this but it has yet to be merged)
You have to create a Virtual Host on your local webserver and point the document root of that to the public directory. After that you can access your app with something like: http://lumen.dev.
Guide for Virtual Hosts with nginx
Guide for Virtual Hosts with Apache
A simple alternative to setting this up manually is Laravel Homestead. It is an official Vagrant box made for Laravel, that allows you to easily get your development environment up and running.

Resources