Hi, i’m actually develloping an app and i’m having trouble with my routes . To make it simple , I’ve setup a database and manage to read what i’m writing in it using mysql in cmd.
But I still can’t write with the app in the database , because when i use an axios.post() request , it just the index() function of my controller instead of my create() function.
here is what my code look like :
routes.php
<?php
return [
'resources' => [
'menu' => ['url' => '/menus'],
'menu_api' => ['url' => '/api/0.1/menus']
],
'routes' => [
['name' => 'page#index', 'url' => '/', 'verb' => 'GET'],
[
'name' => 'menu_api#preflighted_cors', 'url' => '/api/0.1/{path}',
'verb' => 'OPTIONS', 'requirements' => ['path' => '.+']
]
],
];
MenuController.php
<?php
namespace OCA\SimpleApp\Controller;
use Exception;
use OCA\SimpleApp\AppInfo\Application;
use OCP\IRequest;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Controller;
use OCA\SimpleApp\Service\MenuService;
class MenuController extends Controller
{
/** @var MenuService */
private $service;
use Errors;
public function __construct(IRequest $request, MenuService $service)
{
parent::__construct(Application::APP_ID, $request);
$this->service = $service;
}
/**
* @NoAdminRequired
*/
public function index(): DataResponse
{
return (new DataResponse($this->service->findAll()));
}
/**
* @NoAdminRequired
*/
public function show(int $id): DataResponse
{
return $this->handleNotFound(function () use ($id) {
return $this->service->find($id);
});
}
/**
* @NoAdminRequired
*/
public function create(string $title, string $icon, int $parent, string $link, string $groups, string $position): DataResponse
{
return new DataResponse($this->service->create($title, $icon, $parent, $link, $groups, $position));
}
/**
* @NoAdminRequired
*/
public function update(int $id, string $title, string $icon, int $parent, string $link, string $groups, string $position): DataResponse
{
return $this->handleNotFound(function () use ($id, $title, $icon, $parent, $link, $groups, $position) {
return $this->service->update($id, $title, $icon, $parent, $link, $groups, $position);
});
}
/**
* @NoAdminRequired
*/
public function destroy(int $id)
{
return $this->handleNotFound(function () use ($id) {
return $this->service->delete($id);
});
}
}
I’ve made some tests and it always return whats inside the index() no matter if I use axios.post() or axios.get() functions in my JS app.
should i use something like a restparser to get the data i’m passing with the POST request ?
On the dev documentation they are doing it this way but for me it doesn’t work for some reason I really doesn’t get what I’m doing wrong.
thanks in advance for your help.