Laravel: Remove public from url while also keeping projects separate

亡梦爱人 提交于 2019-12-12 09:08:52

问题


This question seems to bother many beginners (like myself) when installing laravel for the first time. We don't want to have the "localhost/public/" in the url and would rather have a clean url. THIS IS NOT A QUESTION ON HOW TO GET RID OF "public" IN THE URL, as there are already many answers to this question.

My question is "How can I remove the 'public' in the url while also keeping my laravel files in the same directory but also separate from my other projects in my public html/web root folder?"

Current wamp www directory File Structure

www
|-- mynewlaravelproject
|   |-- laravelfolders(non public folders and files)
|   |-- public
|       |-- index.php (Reached at localhost/public)
|       |-- .htaccess
|-- project1
|-- project2

Many solutions are available but it seems that almost all of them have some drawback. Please feel free to correct me on any of the below, as I am asking this question due to a lack of understanding.

1. Move index.php and .htaccess

Move index.php and .htaccess from public to your laravel project root and remove the public folder all together. The problem is that now you are forced to include all of laravel in the publicly displayed folders of your site, which surely defeats the security purpose of separating them right?

    www
    |-- mynewlaravelproject
    |   |-- laravelfolders
    |   |-- index.php (Reached at localhost/)
    |   |-- .htaccess
    |-- project1
    |-- project2

2. Move project files elsewhere, but keep public files in project

Move laravel files to a different non public folder. I moved all of my non-public files into a different directory, leaving the public files behind in the project folder, and then referenced them in my index.php. The problem is that now I have to work with all my controllers and models in a separate directory as my public files, this is cumbersome and requires me to have two text editors open.

    laravelfiles
    |-- laravelfolders (index.php is referencing bootstrap folder within)
    www
    |-- mynewlaravelproject
    |   |-- index.php (Reached at localhost/)
    |   |-- .htaccess
    |-- project1
    |-- project2

3. Move laravel folders to just outside your project

The idea behind this is that your project folder in your web root will act as your public folder, and your laravelfolders will sit in the parent web root directory. This one seems the most reasonable, but the problem is that now my laravel folders and files are mixed in amongst ALL of my other projects in my wamp web root directory. This is messy and difficult to manage files & folders in a text editor.

    www
    |-- mynewlaravelproject(now acts as your public folder)
    |   |-- index.php (Reached at localhost/)
    |   |-- .htaccess
    |-- laravelfolders (This gets messy fast)
    |-- project1 (alongside this...)
    |-- project2 (...and this)

4. Modify your .htaccess OR use a virtual server

The problem is that most people don't recommend modifying your htaccess if at all possible and making a virtual server seems a bit extreme for such a simple task. I don't prefer these options as they seem like workarounds for such a seemingly simple task, I wouldn't be shocked if I'm totally wrong about this one.


回答1:


All of those things are more extreme than using a VirtualHost. This is what virtual hosts are for! You've got one server, and you need it to serve multiple sites from it. So you map multiple domains to the server using your hosts file, then you show a different root folder for each site based on the domain. So in your hosts file:

127.0.0.1        myproject.dev
127.0.0.1        myotherproject.dev

Apache config file:

Listen *:80

NameVirtualHost *:80

<VirtualHost *:80>
ServerName myproject.dev
DocumentRoot "C:/wamp/www/myproject/public"
</VirtualHost> 
<VirtualHost *:80>
ServerName myotherproject.dev
DocumentRoot "C:/wamp/www/myotherproject/public"
</VirtualHost> 

Restart apache and bam! You've got two different laravel projects each with their own domain. I've got ~10 or so different projects arranged like this on my server and it makes it a cinch to deploy new projects.

Sources: http://www.trailheadinteractive.com/creating_multiple_virtual_sites_a_single_apache_install_windows_xp



来源:https://stackoverflow.com/questions/35640558/laravel-remove-public-from-url-while-also-keeping-projects-separate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!