slim-3

PHP Slim 3 Framework - Use MonoLog in Custom Class - Using $this when not in object context

喜欢而已 提交于 2019-12-11 16:38:28
问题 Im am working from the Slim 3 skeleton and trying to use the MonoLog in a custom class I have created which is called Utilities. Utilities.php - which is required from index.php <?php class Utilities { protected $logger; function __construct($c) { $this->logger = $logger; } static function checkPerms() { $this->logger->info("checkPerms() permissions of user id valid."); return true; } } Dependencies.php - I added the following: $container['utilities'] = function ($c) { return new Utilities($c

SlimFramework php v3, withStatus(500) does not work

大兔子大兔子 提交于 2019-12-11 10:45:28
问题 I started learning PHP Slim-Framework v3. But I'm finding it difficult on few occasions. Here is my code: $app = new \Slim\App(["settings" => $config]); $app->get('/', function(Request $request, Response $response, $args = []) { $error = array('result' => false, 'message' => 'Bad Request', 'dev'=>'', 'data' => []); $response->withStatus(500)->getBody()->write(json_encode($error)); }); Now I want to respond with status 500 to the user when ever I have issues in service. But unfortunately this

Slim 3 upload image to server path

安稳与你 提交于 2019-12-11 07:38:05
问题 Hi im new in slim 3 framework (Api for mobile) i would like to know how to upload an image to a server, e seacrh for examples didnt find much, this is the code i have but it does not work.Any help would be great thanks. $app->post('/photo', function ($request, $response) use ($app) { $files = $request->getUploadedFiles(); if (empty($files['newfile'])) { throw new Exception('Expected a newfile'); } $newfile = $files['newfile']; if ($newfile->getError() === UPLOAD_ERR_OK) { $uploadFileName =

New rewrite rules in subdirectory with htaccess

a 夏天 提交于 2019-12-11 06:27:39
问题 I'm struggling with configuring the rewrite rules in the .htaccess files. I'd like to run a Slim PHP App inside my Wordpress folder, so I can reuse the SSL certificate for my API. The Slim App is running inside the /api folder, so it can be accessed by calling https://www.example.com/api . This works well, but if I open an API endpoint like example.com/api/timesheet the page is redirected to the 404 error Wordpress page. What I did so far: I excluded the api directory in the .htaccess file

Slim 3 - Slash as a part of route parameter

安稳与你 提交于 2019-12-10 16:17:53
问题 I need to compose URLs with parameters that can contain a slash /. For example, the classic /hello/{username} route. By default, /hello/Fabien will match this route but not /hello/Fabien/Kris . I would to ask you how can I do it in Slim 3 framework. 回答1: Route placeholders: For “Unlimited” optional parameters, you can do this: $app->get('/hello[/{params:.*}]', function ($request, $response, $args) { $params = explode('/', $request->getAttribute('params')); // $params is an array of all the

Slim 3: how to access settings?

别说谁变了你拦得住时间么 提交于 2019-12-10 02:21:00
问题 Before the Slim 3 is released, codes below work fine: settings.php, return [ 'settings' => [ 'displayErrorDetails' => true, 'modules' => [ 'core' => 'config/core/modules.php', 'local' => 'config/local/modules.php' ], ], ]; index.php // Instantiate the app $settings = require __DIR__ . '/../src/settings.php'; $app = new \Slim\App($settings); $MyClass = new MyClass($app); MyClass.php class MyClass { private $app; public function __construct($app) { $this->app = $app; $local = require $app-

PHP Slim Framework Create Controller

十年热恋 提交于 2019-12-06 07:28:07
问题 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('/',

How to access the $container within middleware class in Slim v3?

空扰寡人 提交于 2019-12-05 11:12:50
问题 I've been reading that in Slim v2, $app was bound to the middleware class. I'm finding this not to be the case in v3? Below is my middleware class, but I'm just getting undefined: <?php namespace CrSrc\Middleware; class Auth { /** * Example middleware invokable class * * @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request * @param \Psr\Http\Message\ResponseInterface $response PSR7 response * @param callable $next Next middleware * * @return \Psr\Http\Message

Slim 3: how to access settings?

夙愿已清 提交于 2019-12-05 02:14:34
Before the Slim 3 is released, codes below work fine: settings.php, return [ 'settings' => [ 'displayErrorDetails' => true, 'modules' => [ 'core' => 'config/core/modules.php', 'local' => 'config/local/modules.php' ], ], ]; index.php // Instantiate the app $settings = require __DIR__ . '/../src/settings.php'; $app = new \Slim\App($settings); $MyClass = new MyClass($app); MyClass.php class MyClass { private $app; public function __construct($app) { $this->app = $app; $local = require $app->settings['modules']['local']; } But after the release, I get this error below: Notice: Undefined property:

JWT: Authentication in slim v3 and Android

半城伤御伤魂 提交于 2019-12-05 01:44:08
问题 I am using Slim framework to return JSON to my Android device. I am currently working on login on my device. I am using 3 different ways to login: Facebook, Google and account login. When he takes account login he can register a new account or login with an existing one. For security on my web service I thought to use JWT security. So I am reading and watching video's about how it works. I think I understand how it works, but I cannot find anything about how to implement it correctly. The