在不使用工匠服务的情况下运行laravel项目的最佳方法是什么?

在不使用工匠服务的情况下运行laravel项目的最佳方法是什么?

问题描述:

guys I have a laravel project and its working fine. Every time I start the project by typing the command php artisan serve and then by typing the url localhost:8000 in my browser. But now I want to start my project only by typing the url localhost/projectname.

So I did a little bit of digging and found out a way to do this, I made it possible by changing the server.php file from root directory to index.php and copying the htaccess file from public folder to root directory`. This way my browser loaded only html file or php file but css files and js files were not loaded so I had to modify there link on the blade file for example:

Previously a style.css file was linked as:

 <link rel="stylesheet" type="text/css" href="{{asset('front_end/assets/css/style.css')}}">

Now I link the same style.css file as below:

<link rel="stylesheet" type="text/css" href="{{asset('/public/front_end/assets/css/style.css')}}">

And It works fine.

But now I have found another problem, I have stored some files (images) in the the storage/app/public/myfolder directory so to access the files I had to use the command php artisan storage:link command and to access the files(images) from above mentioned directory from blade file I use the code below:

<img src="{{$url = Storage::url('myfolder/'.$company->logo_name)}}"> 

This code did worked only worked when I used to start my project using php artisan serve command. Now I its not linking so what should I do? And also can you guys tell me the way that I changed the server.php file to index.php is the right way or not? And what you guys do in your real projects?

Thank You.

When deploying a Laravel project you want your nginx or apache configuration to point to your public path of your project. Changing the server.php or index.php files are not necessary.

Apache Config Example:

<VirtualHost *:80>
   ServerName project.dev
   DocumentRoot "/home/vagrant/projects/project/public"
   <Directory "/home/vagrant/projects/project/public">
      AllowOverride all
   </Directory>
</VirtualHost>

As you can see with this apache config example, it points to the public directory of your project.

You can achieve this locally by installing either Apache or nginx on your local machine.

I would recommend following Laravel's documentation guide on using Vagrant's Homestead to get an idea of how this is working before you decide to run this on a server. A link here

There is a couple of options you can take to achieve essentially the same goal. This depends on the machine you are running on and personal preference.

You don't need to modify any file.

You need one of this:

  • Web server installed locally, Apache or Nginx
  • A Vagrant Virtual machine for running the project like in production environemnts like Homestead
  • Valet

If you decide to go with Web Server, you just need to consider to point to project/public folder instead of project.

Doing with one of this options, you won't need to modify the assets url, since public shouldn't be part of your url in any case.

First of all, don't do any tweaks in server.php file, it's fine. To run your project with url like, http://localhost/my-project.dev or simply my http://project.dev, use WAMP or XAMPP (if working on a windows machine) and lamp for linux. You can also checkout Laravel's Homestead.

Then setup virtual host. See this tutorial for setting up virtual host on WAMP - http://www.coderomeos.org/how-to-set-up-virtual-host-on-wamp-server

And don't put your assets in public/assets folder to prevent using storage link.