{"id":18597407,"url":"https://github.com/vkovic/laravel-db-redirector","last_synced_at":"2025-04-10T17:30:59.087Z","repository":{"id":62544929,"uuid":"160536716","full_name":"vkovic/laravel-db-redirector","owner":"vkovic","description":"Manage redirects using database rules. Rules are intended to be very  similar to Laravel default routes, so syntax is pretty easy to comprehend.","archived":false,"fork":false,"pushed_at":"2019-08-22T11:53:20.000Z","size":12,"stargazers_count":13,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-07-31T17:47:23.778Z","etag":null,"topics":["database","laravel","package","redirect","router","routing"],"latest_commit_sha":null,"homepage":null,"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/vkovic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-05T15:14:02.000Z","updated_at":"2023-09-29T12:04:17.000Z","dependencies_parsed_at":"2022-11-02T22:01:10.340Z","dependency_job_id":null,"html_url":"https://github.com/vkovic/laravel-db-redirector","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vkovic%2Flaravel-db-redirector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vkovic%2Flaravel-db-redirector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vkovic%2Flaravel-db-redirector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vkovic%2Flaravel-db-redirector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vkovic","download_url":"https://codeload.github.com/vkovic/laravel-db-redirector/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223442068,"owners_count":17145698,"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":["database","laravel","package","redirect","router","routing"],"created_at":"2024-11-07T01:28:04.389Z","updated_at":"2024-11-07T01:28:05.320Z","avatar_url":"https://github.com/vkovic.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel DB redirector\n\n[![Build](https://travis-ci.org/vkovic/laravel-db-redirector.svg?branch=master)](https://travis-ci.org/vkovic/laravel-db-redirector)\n[![Downloads](https://poser.pugx.org/vkovic/laravel-db-redirector/downloads)](https://packagist.org/packages/vkovic/laravel-db-redirector)\n[![Stable](https://poser.pugx.org/vkovic/laravel-db-redirector/v/stable)](https://packagist.org/packages/vkovic/laravel-db-redirector)\n[![License](https://poser.pugx.org/vkovic/laravel-db-redirector/license)](https://packagist.org/packages/vkovic/laravel-db-redirector)\n\n### Manage HTTP redirections in Laravel using database\n\nManage redirects using database rules. Rules are intended to be very  similar to Laravel default routes, so syntax is pretty easy\nto comprehend.\n\n---\n\n## Compatibility\n\nThe package is compatible with Laravel versions `5.5`, `5.6`, `5.7` and `5.8` and PHP versions `7.1` and `7.2`\n\n## Installation\n\nInstall the package via composer:\n\n```bash\ncomposer require vkovic/laravel-db-redirector\n```\n\nDatabase redirector middleware needs to be added to middleware array:\n\n```php\n// File: app/Http/Kernel.php\n\n// ...\n\nprotected $middleware = [\n    // ...\n    \\Vkovic\\LaravelDbRedirector\\Http\\Middleware\\DbRedirectorMiddleware::class\n];\n```\n\nRun migrations to create table which will store redirect rules:\n\n```bash\nphp artisan migrate\n```\n\n## Usage: Simple Examples\n\nCreating a redirect is easy. You just have to add db record via provided RedirectRule model.\nDefault status code for redirections will be 301 (Moved Permanently).\n\n```php\nuse Vkovic\\LaravelDbRedirector\\Models\\RedirectRule;\n\n// ...\n\nRedirectRule::create([\n    'origin' =\u003e '/one/two',\n    'destination' =\u003e '/three'\n]);\n```\n\nYou can also specify another redirection status code:\n\n```php\nRedirectRule::create([\n    'origin' =\u003e '/one/two',\n    'destination' =\u003e '/three',\n    'status_code' =\u003e 307 // Temporary Redirect\n]);\n```\n\nYou may use route parameters just like in native Laravel routes,\nthey'll be passed down the road - from origin to destination:\n\n```php\nRedirectRule::create([\n    'origin' =\u003e '/one/{param}',\n    'destination' =\u003e '/two/{param}'\n]);\n\n// If we visit: \"/one/foo\" we will end up at \"two/foo\"\n```\n\nOptional parameters can also be used:\n\n```php\nRedirectRule::create([\n    'origin' =\u003e '/one/{param1?}/{param2?}',\n    'destination' =\u003e '/four/{param1}/{param2}'\n]);\n\n// If we visit: \"/one\" we'll end up at \"/four\n// If we visit: \"/one/two\" we'll end up at \"/four/two\"\n// If we visit: \"/one/two/three\" we'll end up at \"/four/two/three\"\n```\n\nChained redirects will also work:\n\n```php\nRedirectRule::create([\n    'origin' =\u003e '/one',\n    'destination' =\u003e '/two'\n]);\n\nRedirectRule::create([\n    'origin' =\u003e '/two',\n    'destination' =\u003e '/three'\n]);\n\nRedirectRule::create([\n    'origin' =\u003e '/three',\n    'destination' =\u003e '/four'\n]);\n\n// If we visit: \"/one\" we'll end up at \"/four\"\n```\n\nWe also can delete the whole chain at once\n(3 previous redirect records in this example):\n\n```php\nRedirectRule::deleteChainedRecursively('/four');\n```\n\nSometimes it's possible that you'll have more than one redirection with\nthe same destination. So it's smart to surround code with try catch block, because exception\nwill be raised in this case:\n\n```php\nRedirectRule::create(['origin' =\u003e '/one/two', 'destination' =\u003e '/three/four']);\nRedirectRule::create(['origin' =\u003e '/three/four', 'destination' =\u003e '/five/six']);\n\n// One more with same destination (\"/five/six\") as the previous one.\nRedirectRule::create(['origin' =\u003e '/ten/eleven', 'destination' =\u003e '/five/six']);\n\ntry {\n    RedirectRule::deleteChainedRecursively('five/six');\n} catch (\\Exception $e) {\n    // ... handle exception\n}\n```\n\n## Usage: Advanced\n\nWhat about order of rules execution when given url corresponds to multiple rules.\nLet's find out in this simple example:\n\n```php\nRedirectRule::create(['origin' =\u003e '/one/{param}/three', 'destination' =\u003e '/four']);\nRedirectRule::create(['origin' =\u003e '/{param}/two/three', 'destination' =\u003e '/five']);\n\n// If we visit: \"/one/two/three\" it corresponds to both of rules above,\n// so, where should we end up: \"/four\" or \"/five\" ?\n// ...\n// It does not have anything to do with rule order in our rules table!\n```\n\nTo solve this problem, we need to agree on simple (and logical) rule prioritizing:\n\n**Priority 1:**\nRules without named parameters have top priority:\n\n**Priority 2:**\nIf rule origin have named parameters, those with less named parameters will have higher priority\n\n**Priority 3:**\nIf rule origin have same number of named parameters, those where named parameters are nearer the\nend of the rule string will have priority\n\nSo lets examine our previous case, we have:\n- \"/one/{param}/three\" =\u003e \"/four\"\n- \"/{param}/two/three\" =\u003e \"/five\"\n\nIn this case both rules have the same number of named params, but in the first rule \"{param}\" is\nnearer the end of the rule, so it will have priority and we'll end up at \"/four\".\n\n---\n\n## Contributing\n\nIf you plan to modify this Laravel package you should run tests that comes with it.\nEasiest way to accomplish this would be with `Docker`, `docker-compose` and `phpunit`.\n\nFirst, we need to initialize Docker containers:\n\n```bash\ndocker-compose up -d\n```\n\nAfter that, we can run tests and watch the output:\n\n```bash\ndocker-compose exec app vendor/bin/phpunit\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvkovic%2Flaravel-db-redirector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvkovic%2Flaravel-db-redirector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvkovic%2Flaravel-db-redirector/lists"}