Giter Club home page Giter Club logo

blast-base-url's Introduction

Blast\BaseUrl

Build Status

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

Introduction

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.

Detection logic is based on zend-http package.

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

Register factory for middleware:

return [
    'dependencies' => [
        // Use 'factories' for services provided by callbacks/factory classes.
        'factories' => [
            Blast\BaseUrl\BaseUrlMiddleware::class => Blast\BaseUrl\BaseUrlMiddlewareFactory::class,
        ],
    ],
];

Add BaseUrlMiddleware to your pipeline before routing:

return [
    'middleware_pipeline' => [
        'pre_routing' => [
            [
                'middleware' => [
                    BaseUrlMiddleware::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. Enabling it requires following additions to your configuration:

return [
    'dependencies' => [
        'invokables' => [
            // setup factory
            Blast\BaseUrl\BasePathHelper::class => Blast\BaseUrl\BasePathHelper::class,            
        ],        
    ],
    'view_helpers => [
        'factories' => [
            'basePath' => Blast\BaseUrl\BasePathViewHelperFactory::class,
        ],
    ],
];

If BasePathHelper is available, BaseUrlMiddleware will automatically configure it during execution. 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" />

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.