slim-3

Slim 3 Middleware Redirect

笑着哭i 提交于 2020-04-10 08:20:53
问题 I want to check if a user is logged in. Therefor I have an Class witch returns true or false. Now I want a middleware which checks if the user is logged in. $app->get('/login', '\Controller\AccountController:loginGet')->add(Auth::class)->setName('login'); $app->post('/login', '\Controller\AccountController:loginPost')->add(Auth::class); Auth Class class Auth { protected $ci; private $account; //Constructor public function __construct(ContainerInterface $ci) { $this->ci = $ci; $this->account =

Slim 3 getParsedBody() always null and empty

☆樱花仙子☆ 提交于 2020-01-15 05:28:12
问题 I'm am using the Slim Framework Version 3 and have some problems. $app-> post('/', function($request, $response){ $parsedBody = $request->getParsedBody()['email']; var_dump($parsedBody); }); result is always: null Can you help me ? 回答1: It depends how you are sending data to the route. This is a POST route, so it will expect the body data to standard form format ( application/x-www-form-urlencoded ) by default. If you are sending JSON to this route, then you need to set the Content-type

Access $this inside a route in doesn't work “Using $this when not in object context”

≯℡__Kan透↙ 提交于 2020-01-07 05:43:35
问题 I'm trying to use $this inside a function of a route, when I'm doing this, it gives me the following error: Using $this when not in object context Here's the code: function api($request, $response) { $response->write('REST API v1'); $this->logger->addInfo("Something interesting happened"); return $response; } $app = new \Slim\App(); /** my routes here **/ $app->get('/', 'api'); $app->run(); I have tried to implement it based on this. Why it doesn't work to use $this inside the function and

Slim3 exclude route from CSRF Middleware

雨燕双飞 提交于 2020-01-01 07:02:11
问题 I'm building a webshop based on the slim3 framework. I need to handle a server-to-server POST-request to confirm if payment was successful. I added csrf to the container like this: $container['csrf'] = function($container) { return new \Slim\Csrf\Guard; }; and added it to the app like this: $app->add($container->csrf); And it works good. But now i need to be able to add an exception to a certain route so i get the post reques they are sending. I'couldn't find a working solution so far. Any

Redirect to route with GET parameters

风格不统一 提交于 2019-12-24 06:08:29
问题 I'd like a route that parses and puts together an array of GET parameters to redirect to another route that expects GET parameters. I had hoped this would work, where I pass $search_params as part of the pathFor() method: // SEARCH VIEW $app->get('/search', function ($request, $response, $args) { $api = $this->APIRequest->get($request->getAttribute('path'),$request->getQueryParams()); $args['data'] = json_decode($api->getBody(), true); return $this->view->render($response, 'search.html.twig',

Eloquent paginate function in Slim 3 project using twig

十年热恋 提交于 2019-12-24 00:20:13
问题 How can I use paginate function from Eloquent in Slim 3 project using twig ? This is in my controller : $posts = Sound::paginate(2); $this->container->view->render($response, 'admin/sounds/index.twig', [ 'posts' => $posts ]); This is the view : {{ posts.links() }} But it doesn't work as well as I expected : Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in **PATH_TO_PROJECT**\vendor\illuminate\pagination\AbstractPaginator.php on line 412 Fatal

Redirecting with error on Slim Framework

半世苍凉 提交于 2019-12-23 22:48:01
问题 I want to redirect to a page (error.php, or maybe 404/406.php, whatever the error is) depending on the info in a form in my website. I managed to log an error like this: if ($date > $curdate) { return $response ->withStatus(406) ->withHeader('Content-Type', 'text/html') ->write('You can\'t select dates in the future!'); } How can I do it so it sends you to a page with that error in particular instead of logging/requesting it in the network tab? Edit for further explanation : Right now I get

How to redirect and store data for the request after the redirect

断了今生、忘了曾经 提交于 2019-12-12 02:46:14
问题 I am trying to redirect the user to the login page with errors and a flash message. Currently I'm doing this: return $this->container->view->render($response,'admin/partials/login.twig',['errorss'=>$errors]); But I want to redirect to the login page, while still having the errror messages and the flash message. I tried this way but does not work: $this->container->flash->addMessage('fail',"Please preview the errors and login again."); return $response->withRedirect($this->container->router-

Optional parameters in url - Slim 3

我只是一个虾纸丫 提交于 2019-12-12 02:13:46
问题 I have a pretty straightforward question. I am using Slim 3 to build a RESTfull api. How come this works: $app->get('/news[/{params:.*}]', function ($request, $response, $args) { $params = explode('/', $request->getAttribute('params')); $response->write("news!"); return $response; }); But not this: $app->get('/news[/{params:.*}]/details', function ($request, $response, $args) { $params = explode('/', $request->getAttribute('params')); $response->write("news details"); return $response; }); In

Get POST PUT parameters with Slim 3

依然范特西╮ 提交于 2019-12-12 01:43:24
问题 I'm trying to build a full REST API with Slim 3. It was quite easy with Slim 2. But now I've got some issues. The POST and PUT route does not work has expected. I can't get the parameters. I found the $request->getHeaders() on the documentations, which works but instead of getting for exemple the parameter length , I get HTTP_LENGHT and every parameters has this HTTP_ prefixe. I found this question but $request->getParsedBody() return an empty array for me. I'm testing my API with Advanced