slim-3

Slim 3 - How to add 404 Template?

与世无争的帅哥 提交于 2019-12-04 13:14:18
问题 In Slim 2, I can over write the default 404 page easily, // @ref: http://help.slimframework.com/discussions/problems/4400-templatespath-doesnt-change $app->notFound(function () use ($app) { $view = $app->view(); $view->setTemplatesDirectory('./public/template/'); $app->render('404.html'); }); But in Slim 3, // ref: http://www.slimframework.com/docs/handlers/not-found.html //Override the default Not Found Handler $container['notFoundHandler'] = function ($c) { return function ($request,

PHP Slim Framework Create Controller

戏子无情 提交于 2019-12-04 11:43:20
I am creating an API using the Slim framework. Currently I use a single file to create the route and pass a closure to it: $app->get('/', function($req, $resp){ //Code... }) But I realise that my file has grown rapidly. What I want to do is use controllers instead, so I will have a controller class and just pass the instance/static methods to the route, like below class HomeController { public static function index($req, $resp){} } and then pass the function to the route $app->get('/', HomeController::index); I tried this, but it does not work, and I wonder if there is a way I can use it to

Controller unit test in slim3

假如想象 提交于 2019-12-04 11:00:34
问题 At the outset, I would like to say - I'm new in unit testing in PHP (phpunit). In my new project (slim3 framework) I would like to test my controllers for example LoginController. My idea is (in unit test method) Create instance of LoginController Mock some services in controller (DI) Execute method which is response for request (in my controllers method __invoke ) My problem is about parameters for __invoke method. In Slim3 callable method for request has two first params: RequestInterface

Slim3 exclude route from CSRF Middleware

南笙酒味 提交于 2019-12-03 21:49:41
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 advice? If you need to exclude one route from a middleware, there are two options: Option 1: group your

Controller unit test in slim3

你。 提交于 2019-12-03 06:50:14
At the outset, I would like to say - I'm new in unit testing in PHP (phpunit). In my new project (slim3 framework) I would like to test my controllers for example LoginController. My idea is (in unit test method) Create instance of LoginController Mock some services in controller (DI) Execute method which is response for request (in my controllers method __invoke ) My problem is about parameters for __invoke method. In Slim3 callable method for request has two first params: RequestInterface $request and ResponseInterface $response How can I create this parameters in my unit test class? I was

How can i create middleware on Slim Framework 3?

为君一笑 提交于 2019-12-01 14:53:29
I read the documentation here about creating middleware. But which folder or file i must be create it? Documentation is not contain this information. Under the src folder i have middleware.php . For example i want to get post information like this: $app->post('/search/{keywords}', function ($request, $response, $args) { $data = $request->getParsedBody(); //Here is some codes connecting db etc... return json_encode($query_response); }); i made this under the routes.php but i want to create class or middleware for this. How can i do? Which folder or file i must be use. Slim3 does not tie you to

How can i create middleware on Slim Framework 3?

纵然是瞬间 提交于 2019-12-01 14:24:38
问题 I read the documentation here about creating middleware. But which folder or file i must be create it? Documentation is not contain this information. Under the src folder i have middleware.php . For example i want to get post information like this: $app->post('/search/{keywords}', function ($request, $response, $args) { $data = $request->getParsedBody(); //Here is some codes connecting db etc... return json_encode($query_response); }); i made this under the routes.php but i want to create

How to send a URL in route parameter?

让人想犯罪 __ 提交于 2019-12-01 12:01:50
问题 I have defined a route like this : $app->map(['GET', 'POST'],'/abc/[{url}]', function ($request, $response, $args) { return $response; })->add(new CustomMiddleware()); Its working fine when I pass a url without http:// but gives me a 404 page not found -Page with http:// or https:// . I have also tried with url encoded string but gives same error : http://localhost/slim/public/index.php/abc/http%3A%2F%2Fstackoverflow.com The requested URL /slim/public/index.php/abc/http://stackoverflow.com

Dependency Injection Slim Framework 3

非 Y 不嫁゛ 提交于 2019-12-01 04:34:07
I'm using Slim Framework 3 to create an API. The app structure is: MVCP (Model, View, Controller, Providers). Is it possible to have Slim Dependency Inject all my classes? I'm using composer to autoload all my dependencies. My directory structure looks like this: /app - controllers/ - Models/ - services/ index.php /vendor composer.json Here's my composer.json file. { "require": { "slim/slim": "^3.3", "monolog/monolog": "^1.19" }, "autoload" : { "psr-4" : { "Controllers\\" : "app/controllers/", "Services\\" : "app/services/", "Models\\" : "app/models/" } } } Here's my index.php file. Again, the

Dependency Injection Slim Framework 3

孤人 提交于 2019-12-01 01:12:33
问题 I'm using Slim Framework 3 to create an API. The app structure is: MVCP (Model, View, Controller, Providers). Is it possible to have Slim Dependency Inject all my classes? I'm using composer to autoload all my dependencies. My directory structure looks like this: /app - controllers/ - Models/ - services/ index.php /vendor composer.json Here's my composer.json file. { "require": { "slim/slim": "^3.3", "monolog/monolog": "^1.19" }, "autoload" : { "psr-4" : { "Controllers\\" : "app/controllers/"