承接 brainstormdevel/basepath 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

brainstormdevel/basepath

Composer 安装命令:

composer require brainstormdevel/basepath

包简介

A URL base path detector for Slim 4

README 文档

README

A URL base path detector for Slim 4.

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Features

  • Support for Apache and the PHP built-in webserver
  • Tested
  • No dependencies
  • Very fast

Supported servers

  • Apache webserver with mod_rewrite and .htaccess
  • PHP build-in webserver

Requirements

  • PHP 7.2+ or 8.0+

Installation

composer require brainstormdevel/basepath

The recommended directory structure:

  • public/ Web server files, the DocumentRoot
    • .htaccess Apache redirect rules for the front controller
    • index.php The front controller
  • .htaccess Internal redirect to the public/ directory

The following steps are necessary for your Slim 4 application:

For Apache we have to "redirect" the web traffic to the front controller in public/index.php.

Create a file: public/.htaccess with this content:

# Redirect to front controller
RewriteEngine On
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

We also need a rule to "redirect" the sub-directories to the front-controller in public/index.php.

Create a second .htaccess file above the public/ directory with this content:

RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]

Usage

Slim 4 integration

Add the BasePathMiddleware after addRoutingMiddleware() to set the basePath before the routing is started.

Example: public/index.php

<?php

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use BrainStorm\BasePath\BasePathMiddleware;
use Slim\Factory\AppFactory;

require_once __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

// Add Slim routing middleware
$app->addRoutingMiddleware();

// Set the base path to run the app in a subdirectory.
// This path is used in urlFor().
$app->add(new BasePathMiddleware($app));

$app->addErrorMiddleware(true, true, true);

// Define app routes
$app->get('/', function (Request $request, Response $response) {
    $response->getBody()->write('Hello, World!');
    return $response;
})->setName('root');

// Run app
$app->run();

Apache usage

  • Start the apache webserver
  • Open your website, e.g. http://localhost or http://localhost/{my-sub-directory} and you should see the message Hello, World!.

PHP built-in webserver usage

  • Open the console and change into the project public/ directory. Then run:
php -S localhost:8000

If you don't start the webserver from the project public/ directory, you have start it with a specific document root directory:

php -S localhost:8000 -t public
  • Open http://localhost:8000 and you should see the message Hello, World!.

Good URLs

The public/ directory is only the DoumentRoot of your webserver, but it's never part of your base path and the official url.

Good URLs:

  • https://www.example.com
  • https://www.example.com/users
  • https://www.example.com/my-app
  • https://www.example.com/my-app/users

Bad URLs:

  • https://www.example.com/public
  • https://www.example.com/public/users
  • https://www.example.com/my-app/public
  • https://www.example.com/my-app/public/users

Retrieving the base path

$basePath = \Slim\Routing\RouteContext::fromRequest($request)->getBasePath(),

Creating a relative url with the base path

$routeParser = \Slim\Routing\RouteContext::fromRequest($request)->getRouteParser();
$url = $routeParser->urlFor('root');

Rendering the base path into a Twig layout template

This example requires slim/twig-view

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <base href="{{ base_path() }}/"/>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>

Support

License

The MIT License (MIT). Please see License File for more information.

brainstormdevel/basepath 适用场景与选型建议

brainstormdevel/basepath 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 655 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 01 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「slim」 「basepath」 「slim4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 brainstormdevel/basepath 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 brainstormdevel/basepath 我们能提供哪些服务?
定制开发 / 二次开发

基于 brainstormdevel/basepath 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 655
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 30
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-08