{"id":20238246,"url":"https://github.com/basilfx/phalcon-httpcode","last_synced_at":"2026-05-07T23:38:45.779Z","repository":{"id":145221092,"uuid":"84797626","full_name":"basilfx/Phalcon-HttpCode","owner":"basilfx","description":"Add exceptions to simplify controller actions within the Phalcon Framework.","archived":false,"fork":false,"pushed_at":"2017-03-13T08:48:05.000Z","size":11,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-03T14:47:57.578Z","etag":null,"topics":[],"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/basilfx.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-13T07:41:15.000Z","updated_at":"2017-03-13T07:41:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"49a8b167-df07-4810-8fcb-8c16ba082931","html_url":"https://github.com/basilfx/Phalcon-HttpCode","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/basilfx/Phalcon-HttpCode","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basilfx%2FPhalcon-HttpCode","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basilfx%2FPhalcon-HttpCode/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basilfx%2FPhalcon-HttpCode/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basilfx%2FPhalcon-HttpCode/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/basilfx","download_url":"https://codeload.github.com/basilfx/Phalcon-HttpCode/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/basilfx%2FPhalcon-HttpCode/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32760705,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-07T02:14:30.463Z","status":"ssl_error","status_checked_at":"2026-05-07T02:14:29.405Z","response_time":62,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-11-14T08:32:38.101Z","updated_at":"2026-05-07T23:38:45.761Z","avatar_url":"https://github.com/basilfx.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Phalcon-HttpCode\nAdd exceptions to simplify controller actions within the Phalcon Framework.\n\n## Introduction\nThe inspiration for this library comes from the Django Framework, where one can throw exceptions to return a certain HTTP response.\n\n```python\ndef view(request):\n    try:\n        model = Model.objects.get(pk=1)\n    except Model.DoesNotExist:\n        raise Http404(\"Model not found.\")\n\n    # Perform action.\n    # ...\n```\n\nWithin the Phalcon Framework, you could accomplish the same:\n\n```php\n\u003c?php\n\nclass ModelController extends Controller\n{\n    public function viewAction()\n    {\n        $model = Model::findByID(1);\n\n        if (!$model) {\n            $this-\u003eresponse-\u003esetStatusCode(404);\n\n            return \"Model not found.\";\n        }\n\n        // Perform action.\n        // ...\n    }\n}\n```\n\nHowever, this becomes cumbersome when multiple actions perform the same lookup steps and return the same response. You could improve this by refeactoring the lookup to a private method, but you still need to check its return value to determine the next action.\n\nExceptions will propagate, which will simplify the actions.\n\n```php\n\u003c?php\n\nuse BasilFX\\HttpCode\\Http\\Response;\n\nclass ModelController extends Controller\n{\n    private function getModel()\n    {\n        $model = Model::findByID(1);\n\n        if (!$model) {\n            throw new Response\\Http404();\n        }\n\n        return $model;\n    }\n\n    public function viewAction()\n    {\n        $model = $this-\u003egetModel();\n\n        // Perform action.\n        // ...\n    }\n}\n```\n\n## Requirements\n* PHP 7.0 or later.\n* Phalcon Framework 3.0 or later.\n\n## Installation\nInstall this dependency using `composer require basilfx/phalcon-httpcode`.\n\nIn your dependency injection setup, register the dispatcher. The dispatcher will re-throw any HttpCode exception. You can catch the `dispatch:beforeHttpCode` event to handle it your own way. You can also extend any status code and provide a `toResponse($response)` method to provide the result.\n\n```php\n\u003c?php\n\nuse BasilFX\\HttpCode\\Mvc\\Dispatcher;\n\n$di-\u003eset(\"dispatcher\", function () use ($di) {\n    $dispatcher = new Dispatcher();\n\n    $eventsManager = $di-\u003egetShared(\"eventsManager\");\n    $dispatcher-\u003esetEventsManager($eventsManager);\n\n    return $dispatcher;\n}, true);\n```\n\nYou can still catch other exceptions using the `dispatcher:beforeException` event, but make sure you pass HttpCode exceptions.\n\n```php\n\u003c?php\n\nnamespace My\\Plugins;\n\nuse Phalcon\\Dispatcher;\nuse Phalcon\\Events\\Event;\nuse Phalcon\\Mvc\\Dispatcher as MvcDispatcher;\nuse Phalcon\\Mvc\\User\\Plugin;\n\nuse BasilFX\\HttpCode\\Http\\Response;\n\nclass ExceptionPlugin extends Plugin\n{\n    public function beforeException(Event $event, MvcDispatcher $dispatcher, \\Exception $exception)\n    {\n        if ($exception instanceof Response\\HttpCode) {\n            return;\n        }\n\n        // ...\n    }\n}\n```\n\n## License\nSee the `LICENSE` file (MIT license).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasilfx%2Fphalcon-httpcode","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbasilfx%2Fphalcon-httpcode","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbasilfx%2Fphalcon-httpcode/lists"}