Lravel 5.1.x独立的公共文件夹& http命名空间用于单独的子域

时间:2016-06-05 05:40:59

标签: laravel-5

我的文件夹结构如下所示

app
    Console
    Events
    Exceptions
    Http
        Classroom
            Controllers
            Middleware
            Requests
            Kernel.php
        Default
            Controllers
            Middleware
            Requests
            Kernel.php
        Homework
            Controllers
            Middleware
            Requests
            Kernel.php
        routes-classroom.php
        routes-default.php
        routes-homework.php
        routes.php
    Jobs
    Listeners
    Policies
    Providers
    User.php
bootstrap
config
database
public
public_classroom
    bootstrap
        cache
        app.php
        autoload.php
public_homework
    bootstrap
        cache
        app.php
        autoload.php
resources
storage
tests
vendor

我的vhsot就像:

cr.classroom.loc => public_classroom
hw.classroom.loc => public_homewrok

我想在访问cr.classroom.loc时访问Http / Classroom / controller,hw.classroom.loc也会发生同样的情况。 我修复了名称空间相应地运行composer dump-autoload,php artisan clear-compiled但没有任何帮助。显示以下错误:

  

未捕获的异常' ReflectionException'消息'班级日志不存在'在/var/www/classroom/vendor/laravel/framework/src/Illuminate/Container/Container.php:741

1 个答案:

答案 0 :(得分:0)

问题出在Route Service Provider上。提供商无法解决请求。将以下代码添加到提供程序。

protected $ar_subdomain_namespaces = [
    '' => 'Home',
    'cr' => 'Classroom',
    'hw' => 'Homework',
    'lms' => 'Lms',
    'admin' => 'Admin',
    ...
];

public function map(Router $router) {
    $this->mapWebRoutes($router);
}

protected function mapWebRoutes(Router $router) {
    if (isset($_SERVER['HTTP_HOST'])) {
        $fqdn = $_SERVER['HTTP_HOST'];
        $ar_host = explode(".", $fqdn);
    }

    $sub_domain = ( isset($ar_host) && count($ar_host) > 2 && $ar_host[0] !== 'www' ) ? $ar_host[0] : '';
    list($namespace, $route_path) = $this->getNamepaceAndRouteBySubdomain($sub_domain);

    $router->route_file_path = $route_path;
    $router->group([
        'namespace' => $namespace, 'middleware' => 'web',
            ], function ($router) {
        require app_path($router->route_file_path);
    });
}

protected function getNamepaceAndRouteBySubdomain($sub_domain = '') {
    $namespace = '\Controllers';
    $route_path = 'Http/Home/routes.php';

    if (array_key_exists($sub_domain, $this->ar_subdomain_namespaces)) {
        $route_path = "Http/{$this->ar_subdomain_namespaces[$sub_domain]}/routes.php";
        $namespace = "{$this->namespace}\\{$this->ar_subdomain_namespaces[$sub_domain]}{$namespace}";
    }

    return [$namespace, $route_path];
}