slim

How to accept sorting and pagination parameters in all REST URIs using Slim?

北城余情 提交于 2019-12-03 17:13:50
I am using the Slim PHP framework to create a RESTful API for my app. I would like all URLs to be able to accept parameters for sorting and pagination. Can someone tell me the best way to do this? Also, can someone provide me with some proper REST URIs to this? (i.e. http://domain.com/api/category/fruit/?sort=DESC&results=25&page=2 ) <?php require 'Slim/Slim.php'; $sort = "ASC"; $results = 10; $page = 1; $app = new Slim(); $app->get('/wines', function () use ($app) { $sort = $app->request()->params('sort'); $results = $app->request()->params('results'); $page = $app->request()->params('page');

How can I add a custom filter to my Twig templates inside of Slim?

落花浮王杯 提交于 2019-12-03 17:03:19
Using the example from http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension : within my main Slim file that creates the view: $filter = new Twig_SimpleFilter( 'stripslashes', function ( $string ) { return stripslashes( $string ); }); $loader = new \Twig_Loader_String(); $twig = new Twig_Environment($loader); $twig->addFilter($filter); $app->view($twig); $app->view()->setData( array( 'nav' => $nav, 'sidenav' => $sidenav, )); Results in: Call to undefined method Twig_Environment::appendData() . Tried in various ways such as this: $app->view(new \Slim\Views\Twig()); $app->view-

Access app in class in Slim Framework 3

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 17:03:05
问题 Im having trouble understanding how to access the instance of Slim when a route is in a seperate class than index.php When using Slim Framework 2 I always used the following, but its not working in Slim 3: $this->app = \Slim\Slim::getInstance(); Im trying to access a database connection I have setup in the container, but from a separate class. This is what I currently got in my index.php to initiate a Slim app: require_once("rdb/rdb.php"); $conn = r\connect('localhost'); $container = new

How to respond in Middleware Slim PHP Framework

有些话、适合烂在心里 提交于 2019-12-03 16:48:58
I am creating middleware for auth into REST API. My API is created using Slim PHP Framework ,which in case provide great features to build APIs. One of this feature is Middleware. I need to check credentials in Middleware and respond with an error (HTTP code with JSON descriptions) to the user. But unfortunatly Slim Framework gives me an exception whenever I try to halt and respond with the HTTP code. <?php require_once __DIR__.'/../Slim/Middleware.php'; class TokenAuth extends \Slim\Middleware { private $auth; const SECURED_URI_REGEX = "/^\/v\d\/store\/(orders|users|payment).*/"; const TOKEN

Replace critical section with SRW lock

Deadly 提交于 2019-12-03 16:32:44
问题 If the application is targeted on Windows Vista or later, could we replace all critical sections with SRW locks? Since critical section is mutually exclusive, for usage it is equivalent to SRW locks in exclusive mode, right? According to MSDN, SRW is optimized both for speed and space. Is there any drawback for doing this? I'm not sure how CS and SRW are implemented internally by Microsoft. Thanks! 回答1: See Joe Duffy's book "Concurrent Programming on Windows", pg 289. The short answer to your

Slim - How to send response with “Content-Type: application/json” header?

蓝咒 提交于 2019-12-03 14:40:26
问题 I have this simple REST api, done in Slim, <?php require '../vendor/autoload.php'; function getDB() { $dsn = 'sqlite:/home/branchito/personal-projects/slim3-REST/database.sqlite3'; $options = array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); try { $dbh = new PDO($dsn); foreach ($options as $k => $v) $dbh->setAttribute($k, $v); return $dbh; } catch (PDOException $e) { $error = $e->getMessage(); } } $app = new \Slim\App(); $app->get('/', function($request,

jQuery ajax() POST to Slim PHP framework

拈花ヽ惹草 提交于 2019-12-03 08:31:27
Using jquery mobile+phonegap, trying to POST to a Slim application, I have this code: $( document ).on( "vclick", "#test_form", function() { $.ajax({ type: "POST", url: "http://mydomain.com/slim/", crossDomain: true, beforeSend: function() { $.mobile.loading('show') }, complete: function() { $.mobile.loading('hide') }, data: {namec:$("#namec").val()}, dataType: 'json', success: function(response) { //console.error(JSON.stringify(response)); alert(response); }, error: function() { //console.error("error"); alert('Not working!'); } }); }); I have tested this with other non Slim PHP pages and

Access app in class in Slim Framework 3

家住魔仙堡 提交于 2019-12-03 06:50:50
Im having trouble understanding how to access the instance of Slim when a route is in a seperate class than index.php When using Slim Framework 2 I always used the following, but its not working in Slim 3: $this->app = \Slim\Slim::getInstance(); Im trying to access a database connection I have setup in the container, but from a separate class. This is what I currently got in my index.php to initiate a Slim app: require_once("rdb/rdb.php"); $conn = r\connect('localhost'); $container = new \Slim\Container; $container['rdb'] = function ($c){return $conn;} $app = new \Slim\App($container); And

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

Replace critical section with SRW lock

耗尽温柔 提交于 2019-12-03 05:56:11
If the application is targeted on Windows Vista or later, could we replace all critical sections with SRW locks ? Since critical section is mutually exclusive, for usage it is equivalent to SRW locks in exclusive mode, right? According to MSDN, SRW is optimized both for speed and space. Is there any drawback for doing this? I'm not sure how CS and SRW are implemented internally by Microsoft. Thanks! Mark oskin See Joe Duffy's book "Concurrent Programming on Windows", pg 289. The short answer to your question is "almost". There are semantics with recursively acquired CRITICAL_SECTION's that are