{"id":20236217,"url":"https://github.com/bakaphp/http","last_synced_at":"2025-10-23T22:03:29.933Z","repository":{"id":49549790,"uuid":"122411995","full_name":"bakaphp/http","owner":"bakaphp","description":"PhalconPHP Http Package to simply the way to create RESTful API","archived":false,"fork":false,"pushed_at":"2021-06-15T01:13:02.000Z","size":280,"stargazers_count":8,"open_issues_count":7,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-24T16:43:38.006Z","etag":null,"topics":["api","cruds","phalcon","phalconphp","php","restful"],"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/bakaphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-22T00:39:18.000Z","updated_at":"2021-06-17T09:44:31.000Z","dependencies_parsed_at":"2022-09-06T09:22:40.568Z","dependency_job_id":null,"html_url":"https://github.com/bakaphp/http","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakaphp%2Fhttp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakaphp%2Fhttp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakaphp%2Fhttp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakaphp%2Fhttp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bakaphp","download_url":"https://codeload.github.com/bakaphp/http/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248278661,"owners_count":21077287,"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":["api","cruds","phalcon","phalconphp","php","restful"],"created_at":"2024-11-14T08:19:34.506Z","updated_at":"2025-10-23T22:03:24.909Z","avatar_url":"https://github.com/bakaphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Baka Http\n\nPhalconPHP package to create fast RESTful API's providing a simple way to create fast CRUD's\n\n## Table of Contents\n1. [Testing](#markdown-header-testing)\n2. [REST CRUD](#markdown-header-routes)\n    1. [Controller Configuration](#markdown-header-controllers)\n3. [QueryParser](#markdown-header-QueryParser)\n4. [QueryParser Extended](#markdown-header-QueryParser-Extended)\n\n## Testing\n```\ncodecept run\n```\n\n## Routes Configuration\n\nTo avoid having to create Controller for CRUD api we provide the \\Baka\\Http\\Rest\\CrudController\n\nAdd to your routes.php\n\n```php\n\u003c?php\n/**\n * Need to understand if using this can be a performance disadvantage in the future\n */\n$defaultCrudRoutes = [\n    'business',\n    'clients',\n    'contacts',\n    'modules',\n    'customFields' =\u003e 'custom-fields',\n    'leads',\n    'products',\n    'productType' =\u003e 'product-type',\n    'users',\n    'sellers',\n];\n\n$router = new RouterCollection($application);\n\nforeach ($defaultCrudRoutes as $key =\u003e $route) {\n\n    //set the controller name\n    $name = is_int($key) ? $route : $key;\n    $controllerName = ucfirst($name) . 'Controller';\n\n    $router-\u003eget('/v1/' . $route, [\n      'Gewaer\\Controllers\\\\' . $controllerName,\n      'index',\n    ]);\n\n    $router-\u003epost('/v1/' . $route, [\n      'Gewaer\\Controllers\\\\' . $controllerName,\n      'create',\n    ]);\n\n    $router-\u003eget('/v1/' . $route . '/{id}', [\n      'Gewaer\\Controllers\\\\' . $controllerName,\n      'getById',\n    ]);\n\n    $router-\u003eput('/v1/' . $route . '/{id}', [\n      'Gewaer\\Controllers\\\\' . $controllerName,\n      'edit',\n    ]);\n\n    $router-\u003edelete('/v1/' . $route . '/{id}', [\n      'Gewaer\\Controllers\\\\' . $controllerName,\n      'delete',\n    ]);\n\n    /**\n    * Mounting routes\n    */\n    $router-\u003emount();\n}\n```\n\nYou can also pass params to the routes to disable JWT and in the future assigne a middleware\n```php\n\u003c?php\n\n$router-\u003esetPrefix('/v1');\n$router-\u003eget('/', [\n    'Gewaer\\Api\\Controllers\\IndexController',\n    'index',\n    'options' =\u003e [\n        'jwt' =\u003e false,\n    ]\n]);\n```\n\n## Controller configuration\n\nAdd\n\n```php\n\u003c?php\n\nclass AnyController extends Baka\\Http\\Rest\\CrudController\n\n/**\n * set objects\n *\n * @return void\n */\npublic function onConstruct()\n{\n    $this-\u003emodel = new Clients();\n    $this-\u003ecustomModel = new ClientsCustomFields();\n}\n```\n\n\n# QueryParser\n\nParse GET request for a API , giving the user the correct phalcon model params to perform a search\n\n```\n//search by fieds and specify the list of fields\nGET - /v1/?q=(searchField1:value1,searchField2:value2)\u0026fields=id_pct,alias,latitude,longitude,category,chofer,phone,coords,last_report\u0026limit=1\u0026page=2\u0026sort=id_pct|desc\n\n//filter by relationships\nGET - /v1/?q=(searchField1:value1,searchField2:value2)\u0026with=vehicles_media[seriesField:value] \n\n//add to the array a relationship of this model\nGET - /v1/?q=(searchField1:value1,searchField2:value2)\u0026with=vehicles_media[seriesField:value]\u0026relationships=direccione\n```\n\n\n```php\n\u003c?php\n\n$parse = new QueryParser($this-\u003erequest-\u003egetQuery());\n$parse-\u003erequest();\n\n[conditions] =\u003e 1 = 1 AND searchField1 = ?1 AND searchField2 = ?2\n[bind] =\u003e Array\n    (\n        [1] =\u003e value1\n        [2] =\u003e value2\n    )\n\n[columns] =\u003e Array\n    (\n        [0] =\u003e id_pct\n        [1] =\u003e alias\n        [2] =\u003e latitude\n        [3] =\u003e longitude\n        [4] =\u003e category\n        [5] =\u003e chofer\n        [6] =\u003e phone\n        [7] =\u003e coords\n        [8] =\u003e last_report\n    )\n\n[order] =\u003e id_pct desc\n[limit] =\u003e 10\n[offset] =\u003e 10\n```\n\n# ~~QueryParser CustomFields~~ (DEPRECATED)\n\nParse GET request for a API , given the same params as before but with cq (for custom domains) , this give out a normal SQL statement for a raw query\n\n`GET - /v1/?q=(searchField1:value1,searchField2:value2)\u0026cq=(member_id:1)\u0026q=(leads_status_id:1)`\n relationship of this model\n\n\n```php\n\u003c?php\n\n$request = $this-\u003erequest-\u003egetQuery();\n$parse = new QueryParserCustomFields($request, $this-\u003emodel);\n$params = $parse-\u003erequest();\n$newRecordList = [];\n\n$recordList = (new SimpleRecords(null, $this-\u003emodel, $this-\u003emodel-\u003egetReadConnection()-\u003equery($params['sql'], $params['bind'])));\n\n//navigate los records\n$newResult = [];\nforeach ($recordList as $key =\u003e $record) {\n\n    //field the object\n    foreach ($record-\u003egetAllCustomFields() as $key =\u003e $value) {\n        $record-\u003e{$key} = $value;\n    }\n\n    $newResult[] = $record-\u003etoFullArray();\n}\n\nunset($recordList);\n```\n\n# QueryParser Extended\n\nThe extended query parser allows you to append search parameters directly via the controller without having to rewrite the function code.\n\nFeatures include the ability to search within a model, within a model's custom fields and within a model's descendant relationships.\n\nParameters are passed in the format `field` `operator` `value`. Valid operators are `:`, `\u003e`, `\u003c`.\n\nMultiple fields can be search by separating them with a `,`. You can search a field by several values by separating said values with `|` (equivalent to SQL's `OR`).\n\n### Query the Model\n`GET - /v1/model?q=(field1:value1,field2:value2|value3)`\n\n### Query the Custom Fields\n`GET - /v1/model?cq=(field1\u003evalue1)`\n\n### Query related Models\nQuerying related models demands a slightly different structure. Each related model that we want queried must be passed as they are named in the system, `_` is used to separate camel cases.\n\n`GET - /v1/model?rq[model_name]=(field1\u003cvalue1|value2)`\n\n### `Between`\nWhile between is strictly not supported at this time, you can produce the same result following this procedure:\n\n`GET - /v1/model?q=(field1\u003evalue1,field1\u003cvalue2)`\n\n### Like, Empty or Not\nThere is another nice feature that you can use to query the model.\n\n#### Like\n* `GET - /v1/model?q=(field1:%value)`\n* `GET - /v1/model?q=(field1:value%)`\n* `GET - /v1/model?q=(field1:%value%)`\n\n#### Empty\nYou can tell the query parser to make sure a field is empty. In the case of integer properties, the query parser will ask the model if the default value for a property is `0`. If it is, it will include `0` as an empty value.\n\n`GET - /v1/model?q=(field1:%%)`\n\n#### Not\nThis is the opposite of the above Empty.\n\n`GET - /v1/model?q=(field1:$$)`\n\n### One for all, and all for One\nYou can use all the above described feature together in one query.\n\n`GET - /v1/model?q=(field1:value1|value2,field2\u003evalue3,field2\u003cvalue4,field3:$$)\u0026cq=(field4:value5)\u0026rq[model_name]=(field5\u003evalue6)`\n\n_Just remember to escape any special character you want to send through a query string to avoid unwanted results._\n\n## Usage\nIn order to access the extended query parser features your controller has to extend from `CrudExtendedController`.\n\n```php\n\u003c?php\n\nclass ExampleController extends \\Baka\\Http\\Rest\\CrudExtendedController\n```\n\nTo append additional search parameters you simply do this:\n```\n\u003c?php\n\npublic function index($id = null): Response\n{\n    $this-\u003eadditionalSearchFields = [\n        ['field', ':', 'value'],\n    ];\n\n    return parent::index();\n}\n```\n\nThis method uses the operators that are passed to the query parser via the URL query. Valid operators are (with their SQL equivalents):\n```php\n\u003c?php\n\n$operators = [\n    ':' =\u003e '=',\n    '\u003e' =\u003e '\u003e=',\n    '\u003c' =\u003e '\u003c=',\n];\n```\n\n# API Custom Fields CRUD\n\nThe CRUD handles the default behavior:\n- GET /v1/leads -\u003e get all\n- GET /v1/leads/1 -\u003e get one\n- POST /v1/leads -\u003e create\n- PUT /v1/leads/1 -\u003e update\n- DELETE /v1/leads/1 -\u003e delete\n\nIn other to use the custom fields you need to extend you controller from CrudCustomFieldsController and define the method `onConstruct()` on this method you define the model of the custom field and the model of the value of this custom fields\n\n```php\n\u003c?php\npublic function onConstruct()\n{\n    $this-\u003emodel = new Leads();\n    $this-\u003ecustomModel = new LeadsCustomFields();\n}\n```\n\nThats it, your controller now manages the custom fields as if they wher properties of the main class\n\n# Normal API CRUD\n\nJust extend your API controller from CrudController and you will have the following functions:\n\nThe CRUD handles the default behaviero:\n- GET /v1/leads -\u003e get all\n- GET /v1/leads/1 -\u003e get one\n- POST /v1/leads -\u003e create\n- PUT /v1/leads/1 -\u003e update\n- DELETE /v1/leads/1 -\u003e delete\n\ncreateFields and updateFields are needed to be define in other to create the field\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbakaphp%2Fhttp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbakaphp%2Fhttp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbakaphp%2Fhttp/lists"}