如何使用Laravel将应用程序路由到多个子域
我使用Laravvel-5.8开发了一个应用程序 我只为一家公司开发了应用程序,其中每个表都有一个从公司表派生的公司id,如下所示:如何使用Laravel将应用程序路由到多个子域,laravel,Laravel,我使用Laravvel-5.8开发了一个应用程序 我只为一家公司开发了应用程序,其中每个表都有一个从公司表派生的公司id,如下所示: class Company extends Model { protected $table = 'companies'; protected $fillable = [ 'id', 'company_name', 'subdomain', ]; } class User extends Authenticatab
class Company extends Model
{
protected $table = 'companies';
protected $fillable = [
'id',
'company_name',
'subdomain',
];
}
class User extends Authenticatable
{
protected $fillable = [
'name',
'company_id',
'email',
];
}
route/web.php如下所示:
Route::get('/', ['as' => '/', 'uses' => 'IndexController@getLogin']);
Auth::routes();
Route::get('/dashboard', 'HomeController@index')->name('dashboard');
// Config Module
Route::group(['prefix' => 'config', 'as' => 'config.', 'namespace' => 'Config', 'middleware' => ['auth']], function () {
Route::resource('countries', 'ConfigCountriesController');
Route::resource('nationalities', 'ConfigNationalitiesController');
});
// HR Module
Route::group(['prefix' => 'hr', 'as' => 'hr.', 'namespace' => 'Hr', 'middleware' => ['auth']], function () {
Route::resource('designations', 'HrDesignationsController');
Route::resource('departments', 'HrDepartmentsController');
Route::resource('employee_categories', 'HrEmployeeCategoriesController');
});
主要路线是:
Route::get('/', ['as' => '/', 'uses' => 'IndexController@getLogin']);
这使得localhost:8888/myapp
config/app.php:
'env' => env('APP_ENV', 'production'),
'url' => env('APP_URL', 'localhost:8888/myapp'),
'asset_url' => env('ASSET_URL', null),
我被要求添加子域,每个子域将根据每个表中的公司id查看数据
localhost:8888/myapp
localhost:8888/company1.myapp
localhost:8888/company2.myapp
所有设备将使用:
Route::get('/', ['as' => '/', 'uses' => 'IndexController@getLogin']);
成功登录后,将重定向到:
Route::get('/dashboard', 'HomeController@index')->name('dashboard');
而且每个人只能看到基于公司id的数据
公司表:
id | company_name | subdomain
1 | Main |
2 | Company1 | company1
3 | Company2 | company2
Main=> localhost:8888/myapp
Company1=>localhost:8888/company1.myapp
Company2=>localhost:8888/company2.myapp
如何修改:
route/web.php
config/app.php
要做到这一点
谢谢有两种方法可以做到这一点 通过中间件 根据您的问题,登录页面URL可以是
localhost:8888/myapp
,localhost:8888/company1.myapp
,或localhost:8888/company2.myapp
使用中间件,我们将在使用中间件的会话中保留公司名称。您可以从会话中使用它
php artisan make:middleware VerifyDomain
将创建一个域
<?php
namespace App\Http\Middleware;
use Closure;
class VerifyDomain
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$domain == "myapp"; // your company app name
$path = $request->getPathInfo(); // should return /company1.myapp or /company2.myapp or /myapp
if (strpos($path, ".") !== false) { // if path has dot.
list($subdomain, $main) = explode('.', $path);
if(strcmp($domain, $main) !== 0){
abort(404); // if domain is not myapp then throw 404 page error
}
} else{
if(strcmp($domain, $path) !== 0){
abort(404); // if domain is not myapp then throw 404 page error
}
$subdomain = ""; // considering for main domain value is empty string.
}
$company = Company::where('company_name', $subdomain)->firstOrFail(); // if not found then will throw 404
$request->session()->put('company_name', $company); //store it in session
return $next($request);
}
}
如果你想申请大多数路由,那么使用中间件应该是最好的选择,而且
localhost:8888/company1.myapp
不是我们所说的子域,子域就像{company\u name}.myapp.com
@bhucho-我现在如何豁免{company u name}。对于主公司,只允许它使用myapp.com您的原始域是什么?localhost one或{company_name}.myapp.com?@bhucho-这是原始域名:localhost:8888/myapp请看这里:id | company|u name | subdomain 1 | Main | 2 | Company1 | Company1 3 | Company2 | Company2 Main=>localhost:8888/myapp Company1=>localhost:8888/Company1.myapp Company2=>localhost:8888/Company2.myapp问题,公司名称是静态的,如您将插入的company1、company2还是将从db表中插入?很抱歉最后一个问题。如问题所示,我现在如何适应不同的群体。例如:Route::group(['prefix'=>'config','as'=>'config','namespace'=>'config','middleware'=>['auth']],function(){和Route::group(['prefix'=>'hr','as'=>'hr','namespace'=>'hr',middleware'=>['auth']],function(){,将其添加为密钥,路由组中的值对如果您使用中间件,然后将其与身份验证中间件一起添加,如果是域,则可以通过域密钥或作为域静态函数直接添加,如上所示如何调用或引用中间件,在route/web.phpadd中验证路由中的域作为数组的值,
Route::domain('localhost:8888/myapp')->group(function () {
Route::get('/', function ($id) {
//
});
});
Route::domain('localhost:8888/{company_name}.myapp')->group(function () {
Route::get('/', function ($company_name, $id) {
$company = Company::where('company_name', $company_name)->firstOrFail();
// send the value of $company to data to send different view data
});
});