一个laravel文件中创建多个应用

来源:互联网 发布:安卓输入法开发源码 编辑:程序博客网 时间:2024/05/16 07:44
服务器采用nginx
在nginx下配置路径
首先会找到laravel文件中的public下的index.php
这里重新创建一个admin(和public在同一个目录)文件,nginx配置路径到admin下的index.php文件,开始模拟在laravel中的另一个应用
将public下的所有文件复制到admin下,并修改index.php
$app = require_once __DIR__.'/../bootstrap/admin.app.php';
此时加载bootstrap下的admin.app.php文件
bootstrap下有app.php文件,复制并且重命名为admin.app.php
修改admin.app.php
$app = new App\Admin\Application(
    realpath(__DIR__.'/../')
);


$app->loadEnvironmentFrom('.env.admin');


$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Admin\Kernel::class
);
通过对这个文件的修改,我们与原来的app.php文件对比
原来的app.php文件:
$app = new Illuminate\Foundation\Application(
    realpath(__DIR__.'/../')
);
$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);
因此我们需要在app目录下添加一个Admin文件,文件下建立继承Illuminate\Foundation\Application的类Application:
<?php


namespace App\Admin;


use Illuminate\Foundation\Application as LaravelApplication;


class Application extends LaravelApplication
{
    /**
     * Get the path to the public / web directory.
     *
     * @return string
     */
公共路径,用来区分访问的是admin还是public,可以在该路径下添加公共资源,css,js,imgaes等
    public function publicPath()
    {
        return $this->basePath . DIRECTORY_SEPARATOR . 'admin';
    }


    /**
     * Get the path to the application configuration files.
     *
     * @return string
     */
由此可发现应该在config目录下配置一份admin,和一份public/web目录
    public function configPath()
    {
        return $this->basePath . DIRECTORY_SEPARATOR . 'config' .
            DIRECTORY_SEPARATOR . 'admin';
    }


    /**
     * Get the path to the language files.
     *
     * @return string
     */
由此可见resources下的lang也需要配置一个admin,一个public/web
    public function langPath()
    {
        return $this->basePath . DIRECTORY_SEPARATOR . 'resources' .
            DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . 'admin';
    }


    /**
     * Get the path to the storage directory.
     *
     * @return string
     */
storage目录下需要配置admin和web
    public function storagePath()
    {
        return $this->storagePath ?: $this->basePath . DIRECTORY_SEPARATOR . 'storage' .
            DIRECTORY_SEPARATOR . 'admin';
    }


    /**
     * Get the path to the configuration cache file.
     *
     * @return string
     */
    public function getCachedConfigPath()
    {
        return $this->basePath().'/bootstrap/admin/cache/config.php';
    }


    /**
     * Get the path to the routes cache file.
     *
     * @return string
     */
    public function getCachedRoutesPath()
    {
        return $this->basePath().'/bootstrap/admin/cache/routes.php';
    }


    /**
     * Get the path to the cached "compiled.php" file.
     *
     * @return string
     */
    public function getCachedCompilePath()
    {
        return $this->basePath().'/bootstrap/admin/cache/compiled.php';
    }


    /**
     * Get the path to the cached services.json file.
     *
     * @return string
     */
    public function getCachedServicesPath()
    {
        return $this->basePath().'/bootstrap/admin/cache/services.json';
    }
}
同时在http下添加Admin文件,建立Kernel.php文件:
复制原有的Kernel.php文件,该文件包含了middleware的路径,admin与public不同
因为不同应用之间的serviceProvider有所不同,所以在Provider目录下创建一个Admin,一个Web目录,分别加载provider,
于此同时,需要修改config下面不同路径中的app.php中serviceprovider类的路径.
因为加载了不同的config配置文件并且不同应用的视图是不相同的,所以试图需要分开建立
在resources/views目录下创建admin文件,这里存放admin应用的视图模板.
于此同时需要修改config/admin下的view.php:
'paths' => [
    realpath(base_path('resources/views/admin')),
],
告诉请求应访问那一个文件下的视图.


不同的应用,使用不同的路由,应此需要创建属于应用自己的路由文件admin.routes.php:
在RouterServiceProvider中提供了访问哪一个路由的映射,该映射函数map中,需要找到应用的routes.php文件,并且需要找到
该应用的路由对应即将访问的controller控制器.
protected $namespace = 'App\Http\Controllers\Admin';
public function map(Router $router)
{
    $router->group(['namespace' => $this->namespace], function ($router) {
        require app_path('Http/admin.routes.php');
    });
}


对应的可以重写artisan文件,新建一个artisan.admin文件:
修改内容
$app = require_once __DIR__.'/bootstrap/admin.app.php';

























0 0
原创粉丝点击