slim

Slim Framework: routes and controllers

杀马特。学长 韩版系。学妹 提交于 2019-12-04 13:47:44
Originally, my Slim Framework app had the classic structure (index.php) <?php $app = new \Slim\Slim(); $app->get('/hello/:name', function ($name) { echo "Hello, $name"; }); $app->run(); But as I added more routes and groups of routes, I moved to a controller based approach: index.php <?php $app = new \Slim\Slim(); $app->get('/hello/:name', 'HelloController::hello'); $app->run(); HelloController.php <?php class HelloController { public static function hello($name) { echo "Hello, $name"; } } This works, and it had been helpful to organize my app structure, while at the same time lets me build

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,

Cannot access Eloquent attributes on Twig

一曲冷凌霜 提交于 2019-12-04 12:07:34
I am trying to access an Eloquent attribute with Twig in Slim, and getting an error. I have a Field and a Type object, and the relationship is as follows class Field extends \Illuminate\Database\Eloquent\Model { protected $table = 'fields'; public function type() { return $this->belongsTo('models\Type'); } When doing {{ f }} (being f a field), the output is this: {"field_id":"1","field_name":"Your name","form_id":"2","type_id":"1","placeholder":"Please give us your name"} And when doing {{ f.type }} , the result is: Message: An exception has been thrown during the rendering of a template (

How can i define global variables in slim framework

可紊 提交于 2019-12-04 11:47:17
How can i define a global variable such that my current_user method can work were ever i want it to, all i simple need to do is check if there is a current user my example code is below if (isset($_SESSION['company_id'])) { $current_user = Companies::find($_SESSION['company_id']); } else { $current_company = null; } how can i use the current user method where ever i want without passing it to my app->render() method just like in my header.html {% if current_user %} hi {{current_user.name}} {% endif %} You can inject a value into the app object: $app->foo = 'bar'; More on Slim’s documentation .

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

Guide me implementing Oauth2 PHP server using thephpleague library

风格不统一 提交于 2019-12-04 09:07:56
I am using Slim Framework With Eloquent ORM . Trying to implement https://github.com/thephpleague/oauth2-server but I am totally confused how to do this. After adding this with composer, I created database with sql file provided in this package. Now it is suggested to implement Storage interfaces . I don't wanna do this, So I just copied storage classes found in Example Folder. I guess they should work as I am using same database right? Also it is unclear how to initially seed the db. Here's my router where I am trying password method. $server = new \League\OAuth2\Server\AuthorizationServer;

Character encoding error for .php file

廉价感情. 提交于 2019-12-04 07:41:00
Have made a route for MarkersController.php which returns json, but when i navigate to the route I get the following error: The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol. My route is as follows: $app->get('/markers/?', function () use ($app) { $controller = new UF\MarkersController($app); return $controller->getMarkersJSON(); });

Axios - How to read JSON response?

那年仲夏 提交于 2019-12-04 00:55:30
Axios 0.17.1 .then(function (response) { console.log(response); //console.log(response.status); //It is an error -> SyntaxError: Unexpected token u in JSON at position 0 console.log(JSON.parse(response.data.error)); console.log(response.data.error); //undefined. The console.log of response is {data: "{"error":"Name must be entered with more than one … NULL↵ ["isPipe":protected]=>↵ NULL↵ }↵}↵", status: 203, statusText: "Non-Authoritative Information", headers: {…}, config: {…}, …} config : {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", …}

How to use Illuminate Database Query Builder & Eloquent in other framework with PHP 5.3.x

可紊 提交于 2019-12-03 20:26:40
I did a couple of projects with Laravel in the past but now I need something very light for a project and went to use Slim, it works great for what I need and I want the great Eloquent ORM and Query Builder of Laravel, can't go without it now :) Now I manage to get it all working with composer, using the information that Taylor displayed on his GitHub, copied his piece of code use Illuminate\Database\Capsule\Manager as Capsule; $capsule = new Capsule; $capsule->addConnection([ 'driver' => 'mysql', 'host' => 'localhost', 'database' => '', 'username' => '', 'password' => '', 'charset' => 'utf8',