slim

PHP Fatal error: Class 'Slim' not found

送分小仙女□ 提交于 2019-11-28 02:16:19
session_start(); date_default_timezone_set('GMT'); require 'Slim/Slim.php'; use Slim\Slim; \Slim\Slim::registerAutoloader(); $app = new \Slim\Slim(); require_once 'item.php'; this is code excerpt from index.php and stuck on the said error when it called item.php . Here the contains of the file $app->put('/getItem', authorize(), 'getItem'); function getItem() { $sql = "SELECT * FROM item"; $app = Slim::getInstance(); try { $db = getConnection(); $stmt = $db->query($sql); $item = $stmt->fetchAll(PDO::FETCH_OBJ); $db = null; $response = $app->response(); $response->header('Content-Type',

Slim framework and GET/PUT/POST methods

北城以北 提交于 2019-11-28 02:13:27
For example, i use this code for testing routes: $app->get('/api', function () { echo 'get!'; }); $app->post('/api', function () { echo 'post!'; }); $app->put('/api', function () { echo 'put!'; }); For api testing i use RestClient plugin for Chrome. When i try do GET request, response is 'get!'. Its good. But: When i try do POST request, response also is 'get!'. Why? Its must be 'post!'. When i try do PUT request, (in Response Headers: Allow: GET,HEAD,POST,OPTIONS,TRACE ) Slim response have 405 error (Method Not Allowed) with message: "The requested method PUT is not allowed for the URL /api."

CORS Post Request Fails

廉价感情. 提交于 2019-11-28 01:19:09
I built an API with the SLIM Micro-Framework. I setup some middleware that adds the CORS headers using the following code. class Cors{ public function __invoke(Request $request, Response $response, $next){ $response = $next($request, $response); return $response ->withHeader('Access-Control-Allow-Origin', 'http://mysite') ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization') ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); } } For my front-end, I used VueJS. I setup VueResource and created a function with

Dependency Injection in Slim Framework - passing Container into your own classes

二次信任 提交于 2019-11-28 01:14:52
问题 I've already commented on this thread but it seems to be dead so I'm opening a new one: Dependency Injection Slim Framework 3 The post above explains how pass Slims Container to a class you've written yourself. However, the OP has asked if it's possible to get Slim to Dependency Inject ALL their classes. I'm also interested in knowing if there's a way to do this since it seems to be anything but DRY if you have to pass the container to every class that you want to use it. As an example, if I

how to use slim redirect

白昼怎懂夜的黑 提交于 2019-11-27 20:33:14
问题 I have a problem. I am using slim and I have route for my main page: $app->get('/', function() use ($app) { ... In one of my controllers I want to redirect to the main page, so I write $app->response->redirect('/', 303); But instead of redirection to the '/' route I'm getting redirected to the root of my local server which is http://localhost/ What am I doing wrong? How should I use redirect method? 回答1: Slim allows you to name routes, and then redirect back to them based upon this name,

Slim Framework - jQuery $.ajax request - Method DELETE is not allowed by Access-Control-Allow-Methods

一曲冷凌霜 提交于 2019-11-27 18:41:48
问题 I am trying to use a REST API written in Slim Framework. Get & Post methods work without any problem. But DELETE requests doesn't work. I get " Method DELETE is not allowed by Access-Control-Allow-Methods " I have already allowed OPTIONS aswell as DELETE in headers. See code below. header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Headers: Content-Type'); header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); $app->options('/(:name+)', function() use(

Slim Framework Base URL

北城余情 提交于 2019-11-27 18:23:55
问题 I'm new to Slim Framework. How to get the base URL like with the Codeigniter function base_url() ? Thanks 回答1: You need to set the base url manually FIRST before you can get it as in this: $app->hook('slim.before', function () use ($app) { $app->view()->appendData(array('baseUrl' => '/base/url/here')); }); http://help.slimframework.com/discussions/questions/49-how-to-deal-with-base-path-and-different-routes 回答2: With Slim v3, as it implements PSR7, you can get a PSR7 Uri object and call the

Slim Framework endpoint unit testing

爱⌒轻易说出口 提交于 2019-11-27 16:28:50
问题 I'm trying to write some PHPUnit tests for my small slim framework app, but don't see anywhere in the docs that point to a way to do a full request and assert on the response (either containing text or a 200 status, or anything, really). Is there any way to do this that anyone has found/used? 回答1: Here is example how you may test your Slim application: https://github.com/mac2000/SlimTestable Suppose we have simple application: <?php use Slim\Slim; require_once 'vendor/autoload.php'; $app =

Intermittent PHP Abstract Class Error

与世无争的帅哥 提交于 2019-11-27 02:03:53
I've been fighting this for a bit, and can't figure it out, maybe someone else has or maybe there's a deeper issue here with Slim, PHP, Apache etc. After working just fine for hours, my Slim install will start giving this on all routes: Fatal error: Class Slim\Collection contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (IteratorAggregate::getIterator) in F:\Projects\example\server\vendor\slim\slim\Slim\Collection.php on line 21 Maddeningly this issue goes away if I restart Apache. (For a few hours anyway.) I found this where someone had a

enable cors in .htaccess

江枫思渺然 提交于 2019-11-26 23:55:57
I have created a basic RESTful service with the SLIM PHP framework and now I'm trying to wire it up so that I can access the service from an Angular.js project. I have read that Angular supports CORS out of the box and all I needed to do was add this line: Header set Access-Control-Allow-Origin "*" to my .htaccess file. I've done this and my REST application is still working (no 500 internal server error from a bad .htaccess) but when I try to test it from test-cors.org it is throwing an error. Fired XHR event: loadstart Fired XHR event: readystatechange Fired XHR event: error XHR status: 0