An open API service indexing awesome lists of open source software.

https://github.com/codad5/php-inex

Simulating Node.js require in php
https://github.com/codad5/php-inex

node-js php php-library

Last synced: 2 months ago
JSON representation

Simulating Node.js require in php

Awesome Lists containing this project

README

          

# PHP IMPORTER

This is a package that simulates node.js import feature.

## Installing it
```
composer require codad5/php-inex
```
## SAMPLE FOR DEFAULT EXPORT

##### Exporting data

- Export.js

```js
export default const greet = () => {
console.log('hello')
}
```

- Export.php

```php
{
console.log('hello')
}
const sum = (a, b) => {
return a + b
}
export {greet, sum}
```

- Export.php

```php
$greet, "sum" => $sum] = Import::this('Export');
$greet(); // hello
echo $sum(2,4) // 6
```

## USING ROUTER LIBRARY

- For php we will use [TrulyAo php-router](https://github.com/aosasona/php-router)
- For Node.js we will use Express.js

##### Exporting routes

- `routes/api.js`

```js
const route = require('express').Router()

route.get('/food', (req, res) => {
res.json({
'name' : "rice"
})
})

export default route
```

- `routes/api.php`

```php
get('/food', function($req, $res){
$res->json([
'name' => "rice"
]);
});
$export = $route;
```

##### Importing routes

- Import.js

```js
const app = require('express')();
const apiRoute = require('routes/api')

app.get('', (req, res) => {
res.send('welcome')
})
app.use('/api', apiRoute)

// serving the application
app.listen(4000)

```

- Import.php

```php
get('/', function($req, $res){
$res->send('welcome');
});
$route->set_route('/api', $apiRoute);

// serving the application
$route->serve();
```

### More
You can also import non php files but you get their file content instead

### Coming soon
- Formatted json import

> Built with 🧡 by [Aniezeofor Chibueze](https://github.com/codad5)