简化laravel框架入口

load();
//实例化服务器容器,注册事件,路由服务提供者
$app = new Illuminate\Container\Container;  //服务容器【服务的注册和解析】
/*foreach ([
             'cache'                => [\Illuminate\Cache\CacheManager::class, \Illuminate\Contracts\Cache\Factory::class],
             'cache.store'          => [\Illuminate\Cache\Repository::class, \Illuminate\Contracts\Cache\Repository::class],
             'config'               => [\Illuminate\Config\Repository::class, \Illuminate\Contracts\Config\Repository::class],
             'files'                => [\Illuminate\Filesystem\Filesystem::class],

         ] as $key => $aliases) {
    foreach ($aliases as $alias) {
        $app->alias($key, $alias);
    }
}*/

with(new Illuminate\Events\EventServiceProvider($app))->register();
with(new Illuminate\Routing\RoutingServiceProvider($app))->register();
\Illuminate\Container\Container::setInstance($app);
app()->instance('cache', new \Illuminate\Cache\CacheManager(app()));
$redisConfig = [

    'client' => 'predis',

    'default' => [
        'host' => getenv('REDIS_HOST', '127.0.0.1'),
        'password' => getenv('REDIS_PASSWORD', null),
        'port' => getenv('REDIS_PORT', 6379),
        'database' => 0,
    ],

];
$client = $redisConfig['client'];
unset($redisConfig['client']);
app()->instance('redis', new \Illuminate\Redis\RedisManager($client, $redisConfig));
$cacheConfig = [
    'default' => getenv('CACHE_DRIVER', 'file'),
    'stores' => [
        'apc' => [
            'driver' => 'apc',
        ],
        'array' => [
            'driver' => 'array',
        ],
        'database' => [
            'driver' => 'database',
            'table' => 'cache',
            'connection' => null,
        ],
        'file' => [
            'driver' => 'file',
            'path' => __DIR__.'/../storage/framework/cache/data',
        ],
        'memcached' => [
            'driver' => 'memcached',
            'persistent_id' => getenv('MEMCACHED_PERSISTENT_ID'),
            'sasl' => [
                getenv('MEMCACHED_USERNAME'),
                getenv('MEMCACHED_PASSWORD'),
            ],
            'options' => [
                // Memcached::OPT_CONNECT_TIMEOUT  => 2000,
            ],
            'servers' => [
                [
                    'host' => getenv('MEMCACHED_HOST', '127.0.0.1'),
                    'port' => getenv('MEMCACHED_PORT', 11211),
                    'weight' => 100,
                ],
            ],
        ],
        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
        ],
    ],

    'prefix' => getenv(
        'CACHE_PREFIX',
        str_slug(getenv('APP_NAME', 'laravel'), '_').'_cache'
    ),
];
app()->instance('config', new \Illuminate\Config\Repository(['cache' => $cacheConfig]));

$database = [
    'driver'    => 'mysql',
    'host'      => getenv('DB_HOST'),
    'port'      => getenv('DB_PORT', '3306'),
    'database'  => getenv('DB_DATABASE'),
    'username'  => getenv('DB_USERNAME'),
    'password'  => getenv('DB_PASSWORD'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
];

//启动Eloquent ORM 模块并进行相关配置
$manager = new Manager();
$manager->addConnection($database); //加载配置
$manager->bootEloquent(); //启动
//加载路由
require __DIR__.'/../app/Http/Routes.php';
//实例化请求并分发处理请求
$request = Illuminate\Http\Request::CreateFromGlobals();
$response = $app['router']->dispatch($request);
//返回请求响应
$response->send();

这是laravel的index.php,基本只保留了路由、控制器、缓存、数据库模型以及少部分配置。本来有个需求是这样的,但是需要再简化,只留下缓存和数据库模型(基本就是纯php了)。心疼我琢磨了好几天,舍不得删,mark在这里看看以后能不能用上

———————————————————————————————————————————————————————

改良后,基本只剩下数据库模型,redis。引入文件建议autoload,因为我试了一个个挑出来,挑了一百多个文件。可能直接引入比autoload少了一些检索时间吧,anyway我也贴出来

load();

//数据库模型
$database = [
    'driver'    => 'mysql',
    'host'      => getenv('DB_HOST'),
    'port'      => getenv('DB_PORT', '3306'),
    'database'  => getenv('DB_DATABASE'),
    'username'  => getenv('DB_USERNAME'),
    'password'  => getenv('DB_PASSWORD'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
];

//启动Eloquent ORM 模块并进行相关配置
$manager = new Manager();
$manager->addConnection($database); //加载配置
$manager->bootEloquent(); //启动

//redis缓存
$redisConfig = [
    'client' => 'predis',
    'default' => [
        'host' => getenv('REDIS_HOST', '127.0.0.1'),
        'password' => getenv('REDIS_PASSWORD', null),
        'port' => getenv('REDIS_PORT', 6379),
        'database' => 0,
    ],
];
$redisManager = new \Illuminate\Redis\RedisManager(\Illuminate\Support\Arr::pull($redisConfig, 'client', 'predis'), $redisConfig);
$redis = new \Illuminate\Cache\Repository(new \Illuminate\Cache\RedisStore($redisManager, false, 'default'));
$redis = $redis->getStore();
//$redis->tags(['api-test'])->put('api','api测试123缓存',1000);
//echo $redis->tags(['api-test'])->get('api');
$user = new \App\Http\Models\User();
$user = $user->find(49)->toArray();

$redis->tags(['api-user'])->put('user49',$user,1000);
var_dump($redis->tags(['api-user'])->get('user49'));

你可能感兴趣的:(个人随笔)