Laravel PHP run php artisan serve automatically if something changes - php

I am using laravel for a php webproject.
Yesterday I noticed an issue:
Everytime I make changes f.e. in the controller files I need to rerun php artisan serve, which consumes a lot of time (close CMD, open it again and type php artisan serve)
I am trying to find a way, so this command will automatically rerun when I test my application on localhost.
I could probably write a script or something with Python, which will automatically open CMD and execute php artisan serve whenever I click f.e. CTRL + F5, but that feels very hacky, is there a cleaner solution?
I also found a guide on how to edit the hosts file in system32 folder and the httpd-vhosts.conf in the apache folder, but after doing all steps, if I try to reach my application I get "It works" instead, which is simply an indicator for a successfull apache installation.

You should avoid php artisan serve cause it's just a shortcut to run your Laravel project. For a better development environment, you should use Laravel Homestead which offers a complete solution.

Related

PHP artisan serve command commits changes very slowly

Whenever I try to update my project in Artisan, the commands I run work, but some of them take a very long time to execute.
For instance, here is a section from my api.php:
Route::apiResource('questions', 'QuestionController');
php artisan serve works in my terminal, and this route is accessible.
But when I delete this questions route, I expect the route host/questions to become inaccessible. However for about a minute after running the command, I can still access the route through the normal URL as if it wasn't deleted.
So what exactly is going on here? Is this caused by caching, and if so, how can I prevent this?
I want a leave-reload thing for my Laravel project.
I'm recompiling my files using ctrl+c, php artisan serve every time, and using Laravel 5.8 with PHP 7.3.7.
This is probably because Opcache keeps a copy of the files for a moment.
Try to disable opcahe and try again.
Check php.ini and see if opcache.enable is on "1", if so, change it to 0, and restart php artisan serve

Linking Storage folder without using Artisan [duplicate]

I want to test some Laravel applications on my basic shared host.
Currently I just upload my complete application including the vendor files, however this takes quite long.
Since I do not have ssh access to my host I'd like to know whether there's an option to run composer / artisan commands without this.
I found this link: Use Composer without ssh access to server (Second Answer) which describes how to run composer using http://phpshell.sourceforge.net/
However, I can change folders in the console etc. But I cannot run php commands - I always get internal server error.
Check if your shared hosting provider has console feature in their CP which allows to run shell commands. Maybe you'll be able to run commands from there.
As alternative, you could right your own artisan runner and call artisan commands from the code:
Artisan::call('migrate');
To run composer command from PHP code, use shell_exec:
shell_exec('composer update');

Deploying Laravel 5.4 to Shared Hosting

I've got a real head-scratcher for you! I've been working on a Laravel 5.4 application for quite some time now and up until yesterday I had been able to:
develop on my local machine [still works flawlessly],
push my changes to my BitBucket repo [still okay here],
and would subsequently pull those changes to my shared hosting server (RedHat) [still running smoothly],
I then run my dependency managers (npm and composer) to get the project in place and functional
there is some matter with clearing various caches:
php artisan view:clear
php artisan route:clear
php artisan cache:clear
composer dump-autoload
and finally move my '/public' folder to the web root and update index.php to point back to the 'bootstrap/autoload.php' in main project structure,
I am aware there is likely another or several steps I am missing, but I am unsure what they are...
All that being said, I've attempted to deploy a number of applications using Laravel lately and I always seem to run into the same issue come time to deploy an application to production. I've read 30+ tutorials on the matter and nothing seems to explain the issue why my site isn't working any more.
I've checked the error log file maintained by Apache, it's empty.
Was wondering if it's a permissions issue, doesn't seem to be the case (all folders set to 775 and files set to 664 as specified by various sources and owned by serverName:userName)
Browser console simply shows a 500 server error.
All I see if "Whoops, looks like something went wrong." twice.
There must be some way to show better error details (config debug setting already set to true)
Any suggestions at this point would be beneficial to send me looking in the right direction!
======= UPDATES =======
For the sake of thoroughness, and that this save others from severe headaches, I'll be posting actions taken here.
Following tutorial mentioned by #user123456 (permissions applies)
Generate new key for application
Run php artisan config:clear
Off to the races, answer to come!
You need to ensure you have a working .env file.
Once done, run php artisan key:generate to create a key for your application after which you should clear your application's cache as follows php artisan config:clear
I would never recommend using shared hosting for Laravel application. You will face lots of issues for permissions, composer and external dependencies. Instead, you can use cloud servers like DigitalOcean, Linode, vultr and deploy laravel application on them. If you don't know about linux and creating Stacks you can use Cloudways to deploy laravel.
https://dev.to/rizwan_saquib/deploy-laravel-application-on-cloud-easily-with-cloudways

Whats the point of running Laravel with the command 'php artisan serve'?

I dont seem to understand why we need to run a Laravel app with php artisan serve vs just running it with Apache or nginx. I know that under development, we use artisan to fire up the site and after deployment to a server, you use the webserver to load up the site.
Whats the use of running the app in artisan in the first place?
The serve command is just a shortcut for the PHP Built-in 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.
One advantage of using php artisan serve over a typical webserver during development is you can use Psysh as a debugger (Laravel Tinker) to set a breakpoint.
For example, at the line of code I want to break at I type:
eval(\Psy\sh());
Then I hit the page that will run that section of code and when it gets to that line it will break into a Psy Shell repl (in the commandline window where I started php artisan serve). Then I can inspect variables, etc. at that point of execution. It's very useful for debugging. As far as I know, you can't do this running Apache/Nginx. It has to be with artisan serve (or running automated tests).
More info here:
https://tighten.co/blog/supercharge-your-laravel-tinker-workflow
http://psysh.org/
Purpose: The purpose of using Php artisan serve (PHP builtin server) is just for testing and easy starting your project it should not be used in real website deployment.
Asset Not working: Always put your index file in public it's the beauty and security of Laravel framework and your assets will always working. if you are bore to use your custom URL like C:/wamp/www/pym/server.php then use Virtual host locally but don't but don't put your index outside the Public folder.
if you really want to use index at your Root directory then you should customize your all asset() and url() helper functions and should put your exact url Example asset('/login') should be changed to asset('localhost/yourprojectroot/login').
php artisan serve --host your_server_ip --port 8000
copy that http://your_server_ip:8000 and run it into the browser
Aside from the best answer here.
You can see the logs directly where you execute the php artisan serve, so useful in debugging.
Well, was looking for the same answer but couldn't find any that is satisfying so , if your also unsatisfied just like me try running the link returned when you run
php artisan serve
it returns
Laravel development server started: <http://127.0.0.1:8000>
copy that /http://127.0.0.1:8000 and run it into the browser , guess what it returns );the page that u first got when you installed laravel for the first time or i guess it will return the page in the routes folder which was set as /home directory or file(default home page).
In brief:
php artisan serve
starts the serve,forexample its like when your going to drive a car and you start the engine before driving whereby you can start the engine and drive at the same time ,its not neccessary to do so but depends.So to me that's php artisan serve CLI.

Laravel - how to serve a new laravel project to browser

This is the first time I've used a PHP framework.
I've been following the Laravel documentation to install Composer and Laravel, and everything seems to have gone smoothly.
But now I've finished the installation/configure instructions, and I have created a project, I can't see any instructions on how to serve my application so it's viewable via a browser?
I have used Ruby on Rails before, which came with an easy way to get an instance of the application running via a built-in web server.
Is there something similar with Laravel, or do I need to somehow configure my standard Apache instance to serve the application?
I'm guessing there is something I've overlooked or misunderstood in the documentation.
yes, you can do it by using following in your terminal.
open your terminal, and navigate to directory where you have your project abc
and fire following command
php artisan serve
Now you can access it in browser by going to http://localhost:8000
hope you get it
I just want to add to the answer of Mubin Khalid . You can choose your own port number like
php artisan serve --port=8080
This is helpful when running two or more project.
You can also serve with you ip address
php artisan serve --host=ip_of_computer_running_laravel --port=8080
works to access server with in same network.
if you are accessing from same laptop you can just browse to ip_of_computer_running_laravel:8080 but if you are browsing from other computer in the network ip_of_computer_running_laravel:8080

Categories