同一域上的多个 Laravel 5 项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37193079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Laravel 5 projects on the same domain
提问by Paulo Manrique
I got several Laravel 5 projects running on subfolders, on the same domain.
我在同一个域的子文件夹上运行了几个 Laravel 5 项目。
Each Laravel application is generating it's own session cookie, and sometimes it generates so much that we get http 400 errors on the entire domain.
每个 Laravel 应用程序都会生成它自己的会话 cookie,有时它会生成太多以至于我们在整个域上都会收到 http 400 错误。
Should I share the storage folder between all those projects, or there's any settings to prevent that to happen?
我应该在所有这些项目之间共享存储文件夹,还是有任何设置可以防止这种情况发生?
采纳答案by Paulo Manrique
Since I found the solution, let me share with you guys.
既然找到了解决办法,就分享给大家。
First, you need to use a shared session driver like Redis or database.
首先,您需要使用共享会话驱动程序,例如 Redis 或数据库。
Second, you need to set on config/session.php, the session cookie and path to be the same.
其次,需要在config/session.php上设置,session cookie和path要一致。
After that, you can run as many different Laravel projects on the same domain without having problems with cookies.
之后,您可以在同一个域上运行多个不同的 Laravel 项目,而不会出现 cookie 问题。
If your application does not require session at all, you can disable sessions entirelly on app/Http/Kernel.php, commenting the following lines:
如果您的应用程序根本不需要会话,您可以在 app/Http/Kernel.php 上完全禁用会话,注释以下几行:
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
回答by TeamRad
Each Laravel installation should be located in its own directory.
每个 Laravel 安装都应位于其自己的目录中。
Then an alias needs to be defined which points the domain sub-folder to the "child" Laravel folder.
然后需要定义一个别名,将域子文件夹指向“子”Laravel 文件夹。
Example in Apache http.conf
:
Apache 中的示例http.conf
:
<VirtualHost some.domain:80>
ServerName some.domain
## Path to laravel domain
DocumentRoot "/path/to/some/domain/laravel-1/public"
<Directory "/path/to/some/domain/laravel-1/public">
AllowOverride All
</Directory>
## Path to laravel sub-folder
Alias /laravel-2-path-alias/ "/path/to/some/domain/laravel-2/public"
<Directory "/path/to/some/domain/laravel-2/public">
AllowOverride All
</Directory>
</VirtualHost>
For session cookies, check config\session.php
in both installations.
对于会话 cookie,请检查config\session.php
两个安装。
Root installation config\session.php
:
根安装config\session.php
:
'cookie' => 'a_unique_name'
'path' => '/',
Subfolder installation config\session.php
:
子文件夹安装config\session.php
:
'cookie' => 'another_unique_name'
'path' => '/path/to/sub/folder',
This should ensure that each installation is writing its own unique session cookies. Any cookies generated by the sub application should not interfere with those of the parent.
这应该确保每个安装都编写自己唯一的会话 cookie。子应用程序生成的任何 cookie 不应干扰父应用程序的 cookie。
回答by Rohit Dhiman
Edit httpd.conf file as
将 httpd.conf 文件编辑为
Please check that path may differ according to your system /opt/lampp/htdocs/or /var/www/html/
请检查路径是否因您的系统而异/opt/lampp/htdocs/或/var/www/html/
## Path to laravel sub-folder
Alias /admin-boilerplate/ "/opt/lampp/htdocs/admin-boilerplate/public/"
<Directory "/opt/lampp/htdocs/admin-boilerplate/public">
AllowOverride All
</Directory>
And you may need to edit .htaccess file in public folder as
您可能需要将公共文件夹中的 .htaccess 文件编辑为
RewriteBase /admin-boilerplate
Full code look like
完整代码看起来像
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# It enables routes when documrnt root set to public foder in httpd.conf file
# change name according to alias name
RewriteBase /admin-boilerplate
</IfModule>
回答by Mohammad Abu Musa
I think you need to have a unique security key for each sub folder and add a special variable for each sub folder go change the generated cookie
我认为您需要为每个子文件夹设置一个唯一的安全密钥,并为每个子文件夹添加一个特殊变量去更改生成的 cookie
回答by Jamie
You should setup Homestead. It's super easy to use. https://laravel.com/docs/5.2/homesteadYou can setup multiple domains for example: domain1.dev, domain2.dev. For each domain you'll have your own cookies.
您应该设置 Homestead。它非常容易使用。https://laravel.com/docs/5.2/homestead您可以设置多个域,例如:domain1.dev、domain2.dev。对于每个域,您将拥有自己的 cookie。