简体   繁体   中英

Laravel Symbolic Links Without Losing Prefix

Hopefully someone out there has encountered this before because we haven't been able to find a solution yet.

Currently, we have two different frameworks installed on our server's root directory ( /home/username ). One of these is a micro framework for serving a very fast API. The other is a Laravel. The directory paths relevant to this question are below:

  • /home/username/public_html (the server's publicly accessible directory)
  • /home/username/microframework/public (the public folder for the micro framework)
  • /home/username/laravel-project/public (the public folder for our Laravel project)

Our goal is to be able to provide multiple prefixes that get routed to the appropriate framework. For example

www.domain.com/api/login --> goes to route /api/login on the micro framework
www.domain.com/account/login --> goes to route /account/login on Laravel
www.domain.com/admin/login --> goes to route /admin/login on Laravel

Attempt #1

The first thing we tried was using symbolic links to direct traffic to the different frameworks

ln -s /home/username/microframework/public/api /home/username/public_html/api
ln -s /home/username/laravel-project/public/account /home/username/public_html/account
ln -s /home/username/laravel-project/public/admin /home/username/public_html/admin

And then in Laravel's routes.php file, we have

Route::get('/account/login', 'AccountController@showLogin');
Route::get('/admin/login', 'AdminController@showLogin');

The issue we have encountered is that we get 403 and 404 errors when using this strategy.

You don't have permission to access /account/login on this server

It seems that we aren't allowed to create a symbolic link that goes to a fake directory (since /home/username/laravel-project/public/account and /home/username/laravel-project/public/admin do not exist). I would have thought this was possible, but it seems not.

Attempt #2

As an alternative, we tried the following approach. We created symbolic links that pointed directly to the framework's public folders.

ln -s /home/username/microframework/public /home/username/public_html/api
ln -s /home/username/laravel-project/public /home/username/public_html/account
ln -s /home/username/laravel-project/public /home/username/public_html/admin

This works, however, we loose the /account and /admin prefixes from our routing. Our Laravel routes.php only worked if it looked like this:

Route::get('/login', 'AccountController@showLogin');
//Route::get('/login', 'AdminController@showLogin'); // this causes conflict since the /login route is already defined

It seems that you can't filter based on the url PRIOR to the public folder, thus the /account and /admin prefixes were not available to define routing. Perhaps there is a setting that would enable this, but we were unable to find it.

So we have two options, but have been unable to get either one working.

If you want to create a symbolic link for the public folder in order to avoid duplication between the public folder on Laravel project and the public folder on you "www" or public_html folder you need to do this: Out project name is : test1-core

So if you went throw this guide: https://medium.com/laravel-news/the-simple-guide-to-deploy-laravel-5-application-on-shared-hosting-1a8d0aee923e you need to have 1 laravel project folder under (1 level up) your "www"/public_html folder. Let's say the name of our laravel project is test1-core. Now By the guide you need to copy the public foler from test1-core to the www folder.

What we need to do is to create a symbolic link with linux to avoid the duplication. Next time you are doing git pull you will not need to copy the public folder to your www folder. First "cd" to your www fodler:

cd ~
cd www

Second make the link:

ln -s ../test1-core/public

If you are on shared hosting and you have multiple folders on your www folder I recommend to named the link:

ln -s ../test1-core/public test1

Link this you will have test1 "linked folder" and you can redirect a domain to it.

Godd luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM