selective/basepath
Composer 安装命令:
composer require selective/basepath
包简介
A URL base path detector for Slim 4
README 文档
README
A URL base path detector for Slim 4.
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 8.2 - 8.5
Installation
composer require selective/basepath
The recommended directory structure:
public/Web server files, the DocumentRoot.htaccessApache redirect rules for the front controllerindex.phpThe front controller
.htaccessInternal 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 Selective\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://localhostorhttp://localhost/{my-sub-directory}and you should see the messageHello, 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:8000and you should see the messageHello, World!.
Good URLs
The public/ directory is only the DocumentRoot of your webserver,
but it's never part of your base path and the official url.
Good URLs:
https://www.example.comhttps://www.example.com/usershttps://www.example.com/my-apphttps://www.example.com/my-app/users
Bad URLs:
https://www.example.com/publichttps://www.example.com/public/usershttps://www.example.com/my-app/publichttps://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
- Issues: https://github.com/selective-php/basepath/issues
- Here you can donate for this project.
License
The MIT License (MIT). Please see License File for more information.
selective/basepath 适用场景与选型建议
selective/basepath 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 557.52k 次下载、GitHub Stars 达 48, 最近一次更新时间为 2019 年 08 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「slim」 「basepath」 「slim4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 selective/basepath 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 selective/basepath 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 selective/basepath 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Secure session middleware for Slim 3 framework
Zend Framework 2 basePath view helper wrapper.
Useful skeleton for RESTful API development, using PHP and Slim 4.
Adds request-parameter validation to the SLIM 3.x PHP framework
Slim Framework 3 CSRF protection middleware utilities
User interface for lavary/crunz. Integrate Crunz library and funtions: Tabular, monthly or weekly interface to view the scheduled and executed tasks. Quick display of the execution result of the tasks that have been executed (Indicator icons easily show the result). Upload, download, edit or delete
统计信息
- 总下载量: 557.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 49
- 点击次数: 32
- 依赖项目数: 14
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-21