PHPackages                             router/slim - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. router/slim

ActiveApplication[Framework](/categories/framework)

router/slim
===========

Slim Router

v0.0.7(2y ago)1921[2 issues](https://github.com/AmChella/router-slim/issues)MITPHPPHP ^7.2||^8.0CI failing

Since May 13Pushed 2y ago2 watchersCompare

[ Source](https://github.com/AmChella/router-slim)[ Packagist](https://packagist.org/packages/router/slim)[ Docs](https://github.com/AmChella/router-slim)[ RSS](/packages/router-slim/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (2)Dependencies (9)Versions (14)Used By (0)

Slim-Router
===========

[](#slim-router)

[![Latest Stable Version](https://camo.githubusercontent.com/61f508ab9072b3bb0c9d74e62471eabb8a1dac887b20ef8716ceddbef16ede8c/68747470733a2f2f706f7365722e707567782e6f72672f726f757465722f736c696d2f762f737461626c653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/router/slim)[![Total Downloads](https://camo.githubusercontent.com/38aec26a16e7cb4b1b4027fb359f01060644b3bd1c4c6fa8c7ad45189f406770/68747470733a2f2f706f7365722e707567782e6f72672f726f757465722f736c696d2f646f776e6c6f6164733f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/router/slim)[![Latest Unstable Version](https://camo.githubusercontent.com/ed924bc73a2625b74a6562b0d686d3fd85d245e7f6a905f004fe33d406d53023/68747470733a2f2f706f7365722e707567782e6f72672f726f757465722f736c696d2f762f756e737461626c653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/router/slim)[![License](https://camo.githubusercontent.com/2d2fa87288da29d82f9d6214c8e5b42d798148676590f440848e06c096140c07/68747470733a2f2f706f7365722e707567782e6f72672f726f757465722f736c696d2f6c6963656e73653f666f726d61743d666c61742d737175617265)](https://packagist.org/packages/router/slim)[![PHP Composer & unit test](https://github.com/AmChella/router-slim/actions/workflows/ci.yml/badge.svg)](https://github.com/AmChella/router-slim/actions/workflows/ci.yml)

application with Slim Framework

### Installation

[](#installation)

Use [Composer](https://getcomposer.org/)

### Do Composer Require as below

[](#do-composer-require-as-below)

```
composer require router/slim {version}

```

### Create instance

[](#create-instance)

```
use \Cs\Router\Util\App as Router;

$app = new Router(
    $containerServices,
    $routes, $cors -> [array] optional
);
$app->run();

```

##### Note: The router will support `GET`|`POST`|`PUT`|`OPTIONS` alone

[](#note-the-router-will-support-getpostputoptions-alone)

---

### Create a routes as below and send the routes as array.

[](#create-a-routes-as-below-and-send-the-routes-as-array)

```
-
  url: /test[/{return}] (optional)
  method: get|post|put|head|delete
  routeBeforeInvoke:
    - [class]->[function]
    ...
  invoke: [class]->[function]
  return: json|raw|download (*optional)

```

- **`url`** is the path of route
- **`method`** is http method like get|post. you can specify it either small or caps
- **`routeBeforeInvoke`** is a special routing like middleware(optional). this is will be invoked before actual method call and the response will to forwared with method|function name as **`array`**.
- **`invoke`** is callback placeholder. *`class`* is a service name and *`function`* is callback method. If you were using DI container then provide full path of the service name.
- **`[/{return}]`** is a optional routing, which is used to return a response as *`json`* , *`raw`*, *`download`*. You can specify it either uri routing or explicitly in array as mentioned above.
- *`download`* is will force the response as downloadable request. Note\* its return values should be like below in the response section.

---

### Your service Response should like below

[](#your-service-response-should-like-below)

---

##### If return type is `Download` then the method response should be Array like below

[](#if-return-type-is-download-then-the-method-response-should-be-array-like-below)

```
[
  'file' => File Content,
  'fileSize' => File Size,
  'ContentType' => mime,
  'fileName' => FileName (in what name it should be downloaded),
  'statusCode' => integer (optional)
]

```

##### If return type is `Download` then the method response should be array like below

[](#if-return-type-is-download-then-the-method-response-should-be-array-like-below-1)

```
[
  'statusCode' => integer (optional),
  'data' => data [array]
]

```

##### If return type is `raw` then the method response should be a string like below

[](#if-return-type-is-raw-then-the-method-response-should-be-a-string-like-below)

```
  'response' -> (string)

```

#### Example 1

[](#example-1)

```
##### specified, routing level return type & routeBeforeIvoke is a kind of middleware it should be called before actual routing method and their reponses are returned in their own method name.
-
  url: /test[/{return}]
  routeBeforeInvoke:
    - test->Validate
    - test->Validate2
  method: post
  invoke: test->welcome
  return: json

req -> /test/json
  * test->Validate get called returns ['Validate' => [response]]
  * test->Validate get called returns  ['Validate' => [response], 'Validate2' => [response]]
  * test->welcome finally called your responses and it should be array of thing bcz, return type is specified as json * [/{return}] -> json *
  Note* static return type will be overrided by dynamic routing (URI routing).
-
  url: /test/{token}[/{return}]
  method: get
  invoke: test->getData
-
  url: /test/download[/{return}] (You should specify return is download)
  method: get
  invoke: test->getFile

```

### Cors values

[](#cors-values)

```
 cors:
    allow_credentials: 'true'
    accept_headers: Content-Type, X-Requested-With
    origin:
        - http://domain.name.com

```

---

### Router request body should like below

[](#router-request-body-should-like-below)

---

##### Request body -&gt; `GET`

[](#request-body---get)

```
uri: test/{name}?job=test
method: get

```

```
[
  'headers' => [
    'headers'
  ],
  'params' => [
    'name' => 'chella',
    'job' => 'test'
  ]
]

```

##### Request body -&gt; `POST`

[](#request-body---post)

```
uri: test/{name}
method: post

```

```
[
  'data' => [
    'post data'
  ],
  'headers' => [
    'headers'
  ],
  'params' => [
    'name' => 'chella
  ]
]

```

##### Request body -&gt; `PUT`

[](#request-body---put)

```
uri: test/{name}
method: put

```

```
[
  'data' => 'stream',
  'headers' => [
    'headers'
  ],
  'params' => [
    'name' => 'chella
  ]
]

```

##### Request body -&gt; `POST`

[](#request-body---post-1)

```
uri: test/{name}
method: post (fileupload)

```

```
[
  'data' => [
    'postdata'
  ],
  'headers' => [
    'headers'
  ],
  'params' => [
    'name' => 'chella
  ],
  'files' => [
      [
       'file' => fileStream,
       'name' => fileName,
       'mime' => mediaType,
       'size' => size
      ]
      [
        ...
      ]
  ]
]

```

#### Example

[](#example)

```
