Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate into OXID, basic object loading, README
- Loading branch information
Showing
6 changed files
with
197 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
<?php | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Create The Application | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters