mtymek/blast-base-url 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

mtymek/blast-base-url

最新稳定版本:1.0.0

Composer 安装命令:

composer require mtymek/blast-base-url

包简介

PSR-7 middleware and helpers for working with base URL.

README 文档

README

Build Status

This package detects base URL of web application. It is useful when you need your app to be served from subdirectory (like http://localhost/my-project/public). This can be useful sometimes, especially in development environment.

View helpers for working with assets are also provided in the package.

Detection logic is based on zend-http package.

Installation

Installation is supported using Composer:

$ composer require mtymek/blast-base-url

If Zend Component Installer is present, it will automatically update application configuration.

Usage

For simplicity, following instructions are targeting applications based on Zend Expressive Skeleton, assuming that Zend\ServiceManager was selected as DI container.

Blast\BaseUrl is based on PSR-7, so it will work well with other frameworks/dispatchers like Slim3 or Relay, just that wiring process will look different.

Base URL Middleware

Add BaseUrlMiddleware to your pipeline, just before routing middleware (config/pipeline.php file):

// ...
$app->pipe(\Blast\BaseUrl\BaseUrlMiddleware::class);

// ...
$app->pipe(RouteMiddleware::class);

BaseUrlMiddleware will alter path from request URI, stripping base url. It means that even if you access your project from http:/localhost/~user/project/public/index.php/foo/bar, next middleware in the pipe will see the path as /foo/bar.

Additionally, two attributes will be added to ServerRequest, holding base URL and base path:

echo $request->getAttribute(BaseUrlMiddleware::BASE_URL);   
// outputs: /some/subdirectory/index.php

echo $request->getAttribute(BaseUrlMiddleware::BASE_PATH);
// outputs: /some/subdirectory/

Generating URLs

BaseUrlMiddleware is able to automatically configure UrlHelper, so that all URLs generated by this helper will have appropriate prefix. This will be done automatically if UrlHelper is available in service container.

Accessing assets - base path

Another feature provided by this package is base path helper. It can be used to generate URLS for your asset files that work correctly under subdirectory.

If BasePathHelper is available, BaseUrlMiddleware will automatically configure it during execution.

Zend View

You will be able to use following syntax inside zend-view templates:

<link rel="stylesheet" href="<?= $this->basePath('/css/style.css') ?>" />

Depending on your application directory, it will produce something similar to:

<link rel="stylesheet" href="/public_html/my-project/public/css/style.css" />

Twig

You will be able to use following syntax inside twig templates:

<link rel="stylesheet" href="{{ basePath('/css/style.css') }}" />

Depending on your application directory, it will produce something similar to:

<link rel="stylesheet" href="/public_html/my-project/public/css/style.css" />

统计信息

  • 总下载量: 54.06k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 1
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 10
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2015-12-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固