slim

Slim PHP Route in Middleware

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 03:38:12
In Slim is it possible to get the current route within middleware? class Auth extends \Slim\Middleware{ public function call(){ $currentRoute = $this->app->getRoute(); // Something like this? } } I know you can call $app->router()->getCurrentRoute() after the slim.before.dispatch hook is called, but when you call this from middleware it returns a non-object. Any help would be greatly appreciated. Steve Whitfield Yes and no. If you look at the source code for Slim, you will see that registered Middlewares are called in LIFO order when the Slim::run method is called, and then Slim runs it's own

Slim 3 get current route in middleware

谁都会走 提交于 2019-11-30 03:18:58
问题 I want to get the name of the current I route in a middleware class. Previously (in Slim 2.*) you could fetch the current route like so: $route = $this->app->router->getCurrentRoute(); But this function has been removed in the 3.0 version of Slim. I've found the following code in the __invoke method of Slim\App : // Get the route info $routeInfo = $request->getAttribute('routeInfo'); /** @var \Slim\Interfaces\RouterInterface $router */ $router = $this->container->get('router'); // If router

Passing Session to TWIG template

给你一囗甜甜゛ 提交于 2019-11-29 22:58:21
问题 i have a problem when i want to get $_SESSION['session']; in twig template using slim micro Framework. this is my code : <!DOCTYPE html> <html> <head> <title>{{ title }} </title> </head> <body> <p> welcome <?php echo $_SESSION['username']; ?> <p> {{ body }} </p> <a href="http://localhost/slim/public_html/logout">logout</a> </body> </html> i can't get session username with that code. any suggestion how to passing session to twig template? 回答1: You should register session as a twig global, so

Slim 3 - how to get all get/ put/ post variables?

為{幸葍}努か 提交于 2019-11-29 20:36:02
How I can get all get/ put/ post variables like in Slim 2 for Slim 3? Slim 2, $allGetVars = $app->request->get(); $allPutVars = $app->request->put(); $allPostVars = $app->request->post(); How can I do that in Slim 3? And, for example, http://example.com/books/1?title=hello&content=world How can I get the params in title and content in Slim 3 now? Slim 2, $title = $app->request->get('title'); $content = $app->request->get('content'); How can I do that in Slim 3? Get all get/put/post parameters: //GET $allGetVars = $request->getQueryParams(); foreach($allGetVars as $key => $param){ //GET

How to POST backbone model data to DB through Slim php and Paris

 ̄綄美尐妖づ 提交于 2019-11-29 20:00:33
I'm trying to get an understanding of how Backbone.js , Slim PHP and Paris/Idiorm might work together and I'm having trouble completing the flow, starting with model attribute data, all the way to the database. PROBLEM: What exactly gets sent to my server when I do model.save() ? Client-side: Backbone.js var Donut = Backbone.Model.extend({ defaults: { name: null, sparkles: false, creamFilled: false }, url: function() { return '/donut'; } }); var bostonCream = new Donut({ name: 'Bawston Cream', sparkles: true, creamFilled: true }); bostonCreme.save(); // <-- Problem: Not sure what & format this

Api route pattern on the SlimPhp microframework?

依然范特西╮ 提交于 2019-11-29 17:31:42
Is there some pattern of routes and how to write the structure using SlimPhp? Like, I made a api folder with a index.php to store ALL my routes: $app->get('/api/shirt/{id}', function (Request $request, Response $response) { //CODE }); $app->get('/api/some-other-endpoint/{id}', function (Request $request, Response $response) //CODE }); But after some time, I realized that my index file will get pretty big. So, how can I manage my endpoint routes? Using classes, controllers, actions? Where can I find documentation about these particular concepts? I'm using controller's (named Action's in this

How to remove index.php from slim framework URL

半城伤御伤魂 提交于 2019-11-29 14:58:03
问题 i'm trying to remove index.php form an URL: this works http://server/bw/index.php/test this doesn't work http://server/bw/test i try to change .htaccess and watching on web i see that it should be like this: RewriteEngine On RewriteBase /bw/ RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [QSA,L] i try editing it in this way: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [QSA,L] or in this way: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f

Slim PHP: Only catch valid routes with middleware

我的未来我决定 提交于 2019-11-29 13:48:16
I'm writing a REST API with Slim. I have written a small middleware to protect the resources so only authenticated users will be able to access them: <?php class SecurityMiddleware extends \Slim\Middleware { protected $resource; public function __construct($resource) { $this->resource = $resource; } public function call() { //get a reference to application $app = $this->app; //skip routes that are exceptionally allowed without an access token: $publicRoutes = ["/","/login","/about"]; if (in_array($app->request()->getPathInfo(),publicRoutes)){ $this->next->call(); //let go } else { //Validate:

Slim framework - cannot interpret routes with dot

余生颓废 提交于 2019-11-29 12:11:56
Problem Statement I'm currently working on an internal RESTful API, and I'm using our main domain name as an environment identifier. However, I noticed that Slim does not like it at all for a route to have dot in there. Sample case I have a local web server running using PHP's built-in webserver, and I invoked php -S 0.0.0.0:5000 to get it running. Once the web server is up, I have a simple 'hello world' on the index page. Everything's working fine and dandy. I then set up a route as following: $app->get('/:domain/:id', function($domain, $id) { echo $domain . ' ' . $id; } With this, I set up

Multiple routes with the same anonymous callback using Slim Framework

走远了吗. 提交于 2019-11-29 09:24:27
How can I define multiple routes that use the same anonymous callback? $app->get('/first_route',function() { //Do stuff }); $app->get('/second_route',function() { //Do same stuff }); I know I can use a reference to a function which would work, but I'd prefer a solution for using the anonymous function to be consistent with the rest of the codebase. So basically, what I'm looking for is a way of doing something like this: $app->get(['/first_route','/second_route'],function() { //Do same stuff for both routes }); ~ OR ~ $app->get('/first_route',function() use($app) { $app->get('/second_route');/