{"id":13805772,"url":"https://github.com/selective-php/basepath","last_synced_at":"2025-05-05T22:34:07.661Z","repository":{"id":45647203,"uuid":"203542399","full_name":"selective-php/basepath","owner":"selective-php","description":"Base path detector for Slim 4","archived":false,"fork":false,"pushed_at":"2023-09-09T13:26:56.000Z","size":41,"stargazers_count":49,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-02T19:17:13.518Z","etag":null,"topics":["apache","basepath","php","slim","slim4"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/selective-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-08-21T08:38:23.000Z","updated_at":"2024-12-06T05:45:30.000Z","dependencies_parsed_at":"2024-01-03T02:34:25.856Z","dependency_job_id":"2fa43e53-f852-41cc-a90a-4030428f0eea","html_url":"https://github.com/selective-php/basepath","commit_stats":{"total_commits":44,"total_committers":5,"mean_commits":8.8,"dds":0.5,"last_synced_commit":"f0bfd7d69e595aba24ca72f05ea9cea1cac7d4d6"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Fbasepath","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Fbasepath/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Fbasepath/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/selective-php%2Fbasepath/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/selective-php","download_url":"https://codeload.github.com/selective-php/basepath/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233292641,"owners_count":18654092,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apache","basepath","php","slim","slim4"],"created_at":"2024-08-04T01:01:04.732Z","updated_at":"2025-01-10T04:11:37.403Z","avatar_url":"https://github.com/selective-php.png","language":"PHP","readme":"# selective/basepath\n\nA URL base path detector for Slim 4.\n\n[![Latest Version on Packagist](https://img.shields.io/github/release/selective-php/basepath.svg)](https://packagist.org/packages/selective/basepath)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)\n[![Build Status](https://github.com/selective-php/basepath/workflows/build/badge.svg)](https://github.com/selective-php/basepath/actions)\n[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/selective-php/basepath.svg)](https://scrutinizer-ci.com/g/selective-php/basepath/code-structure)\n[![Quality Score](https://img.shields.io/scrutinizer/quality/g/selective-php/basepath.svg)](https://scrutinizer-ci.com/g/selective-php/basepath/?branch=master)\n[![Total Downloads](https://img.shields.io/packagist/dt/selective/basepath.svg)](https://packagist.org/packages/selective/basepath/stats)\n\n\n## Features\n\n* Support for Apache and the PHP built-in webserver\n* Tested\n* No dependencies\n* Very fast\n\n### Supported servers\n\n* Apache webserver with mod_rewrite and .htaccess\n* PHP build-in webserver \n\n## Requirements\n\n* PHP 7.2+ or 8.1+\n\n## Installation\n\n```\ncomposer require selective/basepath\n```\n\nThe recommended **directory structure**: \n\n* `public/`      Web server files, the [DocumentRoot](https://httpd.apache.org/docs/2.4/de/mod/core.html#documentroot)\n  * `.htaccess`   Apache redirect rules for the front controller\n  * `index.php`   The front controller\n* `.htaccess`    Internal redirect to the public/ directory\n\nThe following steps are necessary for your Slim 4 application:\n\nFor **Apache** we have to \"redirect\" the web traffic to the front controller\nin `public/index.php`. \n\nCreate a file: `public/.htaccess` with this content:\n\n```htaccess\n# Redirect to front controller\nRewriteEngine On\n# RewriteBase /\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteRule ^ index.php [QSA,L]\n```\n\nWe also need a rule to \"redirect\" the sub-directories to \nthe front-controller in `public/index.php`. \n\nCreate a second `.htaccess` file above the `public/` directory with this content:\n\n```htaccess\nRewriteEngine on\nRewriteRule ^$ public/ [L]\nRewriteRule (.*) public/$1 [L]\n```\n\n## Usage\n\n### Slim 4 integration\n\nAdd the `BasePathMiddleware` after `addRoutingMiddleware()` to set the basePath before \nthe routing is started. \n\nExample: `public/index.php`\n\n```php\n\u003c?php\n\nuse Psr\\Http\\Message\\ResponseInterface as Response;\nuse Psr\\Http\\Message\\ServerRequestInterface as Request;\nuse Selective\\BasePath\\BasePathMiddleware;\nuse Slim\\Factory\\AppFactory;\n\nrequire_once __DIR__ . '/../vendor/autoload.php';\n\n$app = AppFactory::create();\n\n// Add Slim routing middleware\n$app-\u003eaddRoutingMiddleware();\n\n// Set the base path to run the app in a subdirectory.\n// This path is used in urlFor().\n$app-\u003eadd(new BasePathMiddleware($app));\n\n$app-\u003eaddErrorMiddleware(true, true, true);\n\n// Define app routes\n$app-\u003eget('/', function (Request $request, Response $response) {\n    $response-\u003egetBody()-\u003ewrite('Hello, World!');\n    return $response;\n})-\u003esetName('root');\n\n// Run app\n$app-\u003erun();\n```\n\n### Apache usage\n\n* Start the apache webserver\n* Open your website, e.g. `http://localhost` or `http://localhost/{my-sub-directory}` and you should see the message `Hello, World!`.\n\n### PHP built-in webserver usage\n\n* Open the console and change into the project `public/` directory. Then run:\n\n```\nphp -S localhost:8000\n```\n\nIf you don't start the webserver from the project `public/` directory, you have start it with a specific document root directory:\n\n```\nphp -S localhost:8000 -t public\n```\n\n* Open `http://localhost:8000` and you should see the message `Hello, World!`.\n\n### Good URLs\n\nThe `public/` directory is only the `DocumentRoot` of your webserver, \nbut it's never part of your base path and the official url.\n\n\u003cspan style=\"color:green\"\u003eGood URLs:\u003c/span\u003e\n\n* `https://www.example.com`\n* `https://www.example.com/users`\n* `https://www.example.com/my-app`\n* `https://www.example.com/my-app/users`\n\n\u003cspan style=\"color:red\"\u003eBad URLs:\u003c/span\u003e\n \n* `https://www.example.com/public`\n* `https://www.example.com/public/users`\n* `https://www.example.com/my-app/public`\n* `https://www.example.com/my-app/public/users`\n\n### Retrieving the base path\n\n```php\n$basePath = \\Slim\\Routing\\RouteContext::fromRequest($request)-\u003egetBasePath();\n```\n\n### Creating a relative url with the base path\n\n```php\n$routeParser = \\Slim\\Routing\\RouteContext::fromRequest($request)-\u003egetRouteParser();\n$url = $routeParser-\u003eurlFor('root');\n```\n\n### Rendering the base path into a Twig layout template\n\nThis example requires [slim/twig-view](https://github.com/slimphp/Twig-View)\n\n```twig\n\u003c!DOCTYPE html\u003e\n\u003chead\u003e\n    \u003cmeta charset=\"utf-8\"\u003e\n    \u003cbase href=\"{{ base_path() }}/\"/\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n{% block content %}{% endblock %}\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n## Support\n\n* Issues: \u003chttps://github.com/selective-php/basepath/issues\u003e\n* Here you can [donate](https://odan.github.io/donate.html) for this project.\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n\n","funding_links":[],"categories":["Miscellaneous"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fselective-php%2Fbasepath","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fselective-php%2Fbasepath","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fselective-php%2Fbasepath/lists"}