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

本文详细介绍了如何在Laravel项目中通过配置nginx,创建并运行两个独立的应用,一个位于`public`目录,另一个位于`admin`目录。涉及的步骤包括复制public文件到admin,修改index.php,创建和配置admin.app.php,调整Application类,设置不同应用的资源路径,创建Admin Kernel,分离ServiceProviders,视图和路由文件,以及定制artisan命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

服务器采用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';

























评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值