Skip to content

Commit

Permalink
Integrate into OXID, basic object loading, README
Browse files Browse the repository at this point in the history
  • Loading branch information
smxsm committed Nov 9, 2018
1 parent 7754771 commit 8ed88ba
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 12 deletions.
52 changes: 52 additions & 0 deletions README.md
@@ -1,2 +1,54 @@
# oxid-rest-api
powered by OXID Community Hackathon 2018

This REST API prototype uses the PHP micro framework [Lumen](https://lumen.laravel.com/).

## Installation

Copy the repository files into your OXID source folder:

- source
- rest

You can also clone it from Github with:

```bash
cd source/
git clone git@github.com:OXIDprojects/oxid-rest-api.git rest
```


Add this to your __"source/.htaccess"__ file, right below

```bash
RewriteRule oxseo\.php$ oxseo.php?mod_rewrite_module_is=on [L]
```

```bash
# LUMEN REST start
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
RewriteCond %{REQUEST_URI} .*rest.*
RewriteCond %{REQUEST_URI} !rest\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* rest/public/index.php [L,QSA]
# LUMEN REST end
```

to redirect all requests to "/rest/" to the Lumen REST API.

## Usage

There are _two different type of routes_ - database only routes and "OXID object" routes, which bootstrap the OXID framework.

A plain database route for articles is e.g.:

```bash
http://your-shop.org/rest/v1/articles/
```

An example object route is:

```bash
http://your-shop.org/rest/v1/object/articles/05833e961f65616e55a2208c2ed7c6b8
```
3 changes: 3 additions & 0 deletions app/Article.php
Expand Up @@ -8,6 +8,9 @@ class Article extends Model
{

protected $table = 'oxarticles';
public $incrementing = false;
protected $primaryKey = 'OXID';
protected $fillable = ['OXTITLE', 'OXARTNUM', 'OXACTIVE'];

/**
* The attributes excluded from the model's JSON form.
Expand Down
60 changes: 60 additions & 0 deletions app/Http/Controllers/OxidArticleController.php
@@ -0,0 +1,60 @@
<?php

namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;

// OXID classes
use OxidEsales\Eshop\Application\Model\Article as OxidArticle;

class OxidArticleController extends OxidBaseController
{

/**
* Load full OXID article
*
* @param string $id
* @return void
*/
public function showOneArticleFull($id)
{
$article = oxNew(OxidArticle::class);
// disable lazy loading to get all fields immediately
$article->disableLazyLoading();
if ($article->load($id)) {
$aObject = $this->_oxObject2Array($article);
return response()->json($aObject);
}

return response()->json(Article::find($id));
}

public function showOneArticle($id)
{
return response()->json(Article::find($id));
}

public function create(Request $request)
{
$Article = Article::create($request->all());

return response()->json($Article, 201);
}

public function update($id, Request $request)
{
$Article = Article::findOrFail($id);
$Article->update($request->all());

return response()->json($Article, 200);
}

public function delete($id)
{
Article::findOrFail($id)->delete();

return response('Deleted Successfully', 200);
}

}
65 changes: 65 additions & 0 deletions app/Http/Controllers/OxidBaseController.php
@@ -0,0 +1,65 @@
<?php

namespace App\Http\Controllers;

use App\Article;
use Illuminate\Http\Request;

/**
* Boostrap OXID framework
*/
require_once __DIR__ . '/../../../../bootstrap.php';

class OxidBaseController extends Controller
{
/**
* Convert an OXID object to an array
* Hide some specific fields, for now password and passsalt of oxusers
* @param object $o Oxid object
* @return array
*/
protected function _oxObject2Array($o)
{
$vars = get_object_vars($o);
$a = array();
foreach ($vars as $key => $value) {
$vars = get_object_vars($o);
foreach ($vars as $key => $value) {
if (($pos = strpos($key, '__')) > 0) {
$key = substr($key, $pos + 2);
if ($this->_keyIsBlacklisted($key)) {
continue;
}
$value = $value->getRawValue();
$a[strtoupper($key)] = $value;
}
}
}
return $a;
}
/**
* Convert stdClass to array
* @param object $d Oxid object
* @return array
*/
protected function _objectToArray($d)
{
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
// for recursive call
return array_map(array('OxidBaseController','_objectToArray'), $d);
} else {
// return filtered array
if (is_array($d)) {
foreach ($d as $k => $v) {
$k = strtolower($k);
}
}
return $d;
}
}
}
1 change: 0 additions & 1 deletion public/index.php
@@ -1,5 +1,4 @@
<?php

/*
|--------------------------------------------------------------------------
| Create The Application
Expand Down
28 changes: 17 additions & 11 deletions routes/web.php
Expand Up @@ -12,18 +12,24 @@
*/

$router->get(
'/', function () use ($router) {
return $router->app->version();
}
'/rest/', function () use ($router) {
return $router->app->version();
}
);

$router->group(
['prefix' => 'api'], function () use ($router) {
// oxarticles
$router->get('articles', ['uses' => 'ArticleController@showAllArticles']);
$router->get('articles/{id}', ['uses' => 'ArticleController@showOneArticle']);
$router->post('articles', ['uses' => 'ArticleController@create']);
$router->delete('articles/{id}', ['uses' => 'ArticleController@delete']);
$router->put('articles/{id}', ['uses' => 'ArticleController@update']);
}
['prefix' => 'rest/v1'], function () use ($router) {
// oxarticles
$router->get('articles', ['uses' => 'ArticleController@showAllArticles']);
$router->get('articles/{id}', ['uses' => 'ArticleController@showOneArticle']);
$router->post('articles', ['uses' => 'ArticleController@create']);
$router->delete('articles/{id}', ['uses' => 'ArticleController@delete']);
$router->put('articles/{id}', ['uses' => 'ArticleController@update']);
}
);
$router->group(
['prefix' => 'rest/v1/object'], function () use ($router) {
// Oxid Bootstrap routes
$router->get('articles/{id}', ['uses' => 'OxidArticleController@showOneArticleFull']);
}
);

0 comments on commit 8ed88ba

Please sign in to comment.