Роутинг в Slim Framework post запрос

318
11 октября 2017, 09:03

Ребята нужна помощь. Никак не могу разобраться как сделать Использую Slim Framework и twig Хочу изменять определенную запись в базе данных из базы get'ом все получаю и отображаю, а вот post запрос никак не могу реализовать. вот маршруты:

$app->get('/department/{id}', 'DepartmentController:getEditDepartment')->setName('department.{id}');
$app->post('/department/edit/{id}', 'DepartmentController:postEditDepartment')->setName('department.edit');

Вот мой контроллер

    public function getEditDepartment($request, $response, $args){
    echo $args['id'];
    $data = Departament::find($args['id']);
    echo $request->getQueryParam('test');
    echo $data;
    return $this->view->render($response, 'auth/department/edit.twig', [
        'data' => $data
    ]);
}

public function postEditDepartment($request, $response, $args){
    $validation = $this->validator->validate($request, [
        'name' => v::noWhitespace()->notEmpty()
    ]);
    if ($validation->failed()){
        return $response->withredirect($this->router->pathFor('department.{id}'));
    }
    Departament::find($args['id'])->update('name', $request->getParam('name'));
    $this->flash->addMessage('info', 'Ваш пароль успешно изменён');
    return $response->withredirect($this->router->pathFor('home'));
}

вот верстка

    <div class="row">
    <div class="col-md-6 col-md-offset-3">
        <div class="panel panel-default">
            <div class="panel-heading">Редактирование подразделения</div>
            <div class="panel-body">
                <form action="{{ path_for('department.edit', ['id' => 1])}}" method="post" autocomplete="off">
                    <div class="form-group{{ errors.department ? ' has-error' : '' }}">
                        <label for="id">Идентификатор</label>
                        <input type="text" name="id" id="id" class="form-control" value="{{ data.id }}" disabled>
                    </div>
                    <div class="form-group{{ errors.name ? ' has-error' : '' }}">
                        <label for="name">Подразделение</label>
                        <input type="text" name="department" id="department" class="form-control" value="{{ data.name }}">
                        {% if errors.name %}
                            <span class="help-block">{{ errors.name | first }}</span>
                        {% endif %}
                    </div>
                    <button type="submit" class="btn btn-default">Сохранить</button>
                    {{ csrf.field | raw }}
                </form>
            </div>
        </div>
    </div>
</div>

Как правильно сформировать пути и получать доступ В итоге получаю вот такие ошибки

<body><h1>Slim Application Error</h1><p>The application could not run because of the following error:</p><h2>Details</h2><div><strong>Type:</strong> InvalidArgumentException</div><div><strong>Message:</strong> Missing data for URL segment: id</div><div><strong>File:</strong> D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\Router.php</div><div><strong>Line:</strong> 401</div><h2>Trace</h2><pre>#0 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\Router.php(427): Slim\Router-&gt;relativePathFor('department.{id}', Array, Array) 
#1 D:\Artyom\OSPanel\domains\slim.local\app\Controllers\Department\DepartmentController.php(63): Slim\Router-&gt;pathFor('department.{id}') 
#2 [internal function]: App\Controllers\Department\DepartmentController-&gt;postEditDepartment(Object(Slim\Http\Request), Object(Slim\Http\Response), Array) 
#3 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(41): call_user_func(Array, Object(Slim\Http\Request), Object(Slim\Http\Response), Array) 
#4 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\Route.php(344): Slim\Handlers\Strategies\RequestResponse-&gt;__invoke(Array, Object(Slim\Http\Request), Object(Slim\Http\Response), Array) 
#5 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(122): Slim\Route-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response)) 
#6 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\Route.php(316): Slim\Route-&gt;callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response)) 
#7 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\App.php(476): Slim\Route-&gt;run(Object(Slim\Http\Request), Object(Slim\Http\Response)) 
#8 D:\Artyom\OSPanel\domains\slim.local\app\Middleware\ValidationErrorsMiddleware.php(14): Slim\App-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response)) 
#9 [internal function]: App\Middleware\ValidationErrorsMiddleware-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App)) 
#10 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array(Object(App\Middleware\ValidationErrorsMiddleware), Array) 
#11 [internal function]: Slim\DeferredCallable-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App)) 
#12 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(73): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App)) 
#13 D:\Artyom\OSPanel\domains\slim.local\app\Middleware\OldInputMiddleware.php(14): Slim\App-&gt;Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response)) 
#14 [internal function]: App\Middleware\OldInputMiddleware-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure)) 
#15 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array(Object(App\Middleware\OldInputMiddleware), Array) 
#16 [internal function]: Slim\DeferredCallable-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure)) 
#17 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(73): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure)) 
#18 D:\Artyom\OSPanel\domains\slim.local\app\Middleware\CsrfViewMiddleware.php(17): Slim\App-&gt;Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response)) 
#19 [internal function]: App\Middleware\CsrfViewMiddleware-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure)) 
#20 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array(Object(App\Middleware\CsrfViewMiddleware), Array) 
#21 [internal function]: Slim\DeferredCallable-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure)) 
#22 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(73): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure)) 
#23 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\csrf\src\Guard.php(167): Slim\App-&gt;Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response)) 
#24 [internal function]: Slim\Csrf\Guard-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure)) 
#25 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array(Object(Slim\Csrf\Guard), Array) 
#26 [internal function]: Slim\DeferredCallable-&gt;__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure)) 
#27 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(73): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure)) 
#28 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(122): Slim\App-&gt;Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response)) 
#29 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\App.php(370): Slim\App-&gt;callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response)) 
#30 D:\Artyom\OSPanel\domains\slim.local\vendor\slim\slim\Slim\App.php(295): Slim\App-&gt;process(Object(Slim\Http\Request), Object(Slim\Http\Response)) 
#31 D:\Artyom\OSPanel\domains\slim.local\public\index.php(7): Slim\App-&gt;run() 
#32 {main}</pre></body>
Спасибо заранее

READ ALSO