I don't find my error. I can't access of the session variable.
I try this:
$app = new Silex\Application();
$app['debug']=true;
$app->register(new Silex\Provider\SessionServiceProvider());
$app->get("/", function(Request $request) use($app)
{
return $app["session"]->get('is_admin');
});
$app->run();
use Symfony\Component\HttpFoundation\Request;
$app->before(function (Request $request, Silex\Application $app){
$app['session']->set('is_admin', true);
});
I did this to solve the problem (thank you Stepashka):
$app = new Silex\Application();
$app['debug']=true;
use Silex\Provider\SessionServiceProvider;
$app->register(new SessionServiceProvider());
$app->before(function() use ($app){
$app['session']->start();
$app['session']->set('is_admin', "dfghjdf");
});
$app->get("/", function() use($app)
{
return print_r($app['session']->get('is_admin'));
});
$app->run();
Thing is that your providers are not initialised before you do $app->run(). You should assign the value either in controller or at least after $app->run().
There also is a Middleware feature in Silex, so you can do your assignment like this:
$app->before(function (Request $request, Application $app) {
$app['session']->set('is_admin', true);
});
Related
Let's say I have a controller called TeamsController. Controller has following method, that returns all teams user has access to.
public function findAll(Request $request): JsonResponse
{
//...
}
Then I have bunch of other controllers with the same method. I would like to create a single route, that would work for all controllers, so I would not need to add a line for each controller every time I create a new controller.
I am unable to catch the controller name from URI. This is what I have tried.
$router->group(['middleware' => 'jwt.auth'], function () use ($router) {
// This works
//$router->get('teams', 'TeamsController#findAll');
// This just returns TeamsController#findAll string as a response
$router->get('{resource}', function ($resource) {
return ucfirst($resource) . 'Controller#findAll';
});
});
You return a string instead of calling a controller action:
I believe Laravel loads the controllers this way (not tested)
$router->group(['middleware' => 'jwt.auth'], function () use ($router) {
$router->get('{resource}', function ($resource) {
$app = app();
$controller = $app->make('\App\Http\Controllers\'. ucfirst($resource) . 'Controller');
return $controller->callAction('findAll', $parameters = array());
});
});
But again, I don't really think it's a good idea.
Im trying to retrieve body parameters from a POST request using Slim Framework, but it's just returning null and i don't know why.
My structure follows:
Index.php (router file)
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\StreamInterface;
use Slim\Factory\AppFactory;
use Api\Middleware\Auth;
require __DIR__ . '../vendor/autoload.php';
$app = AppFactory::create();
$app->post('/', function (Request $request, Response $response, array $args) {
$parsedBody = $request->getParsedBody();
$response->getBody()->write(json_encode($parsedBody));
return $response;
})->add(new Auth);
$app->run();
Request Structure
{
"test": "testing"
}
Return
null
I'm working with Slim 3 and for rendering I'm using PHP-View. I'm iniating the renderer like this:
...
$container['view'] = new \Slim\Views\PhpRenderer("../mytemplatesfolder/");
$app = new \Slim\App();
$container = $app->getContainer();
$container['renderer'] = new PhpRenderer("templates");
I can render the templates without any problem in my routes, like this:
$app->get('/someroute', function (Request $request, Response $response){
return $this->renderer->render($response, "/onetemplate.phtml");
});
How can I render a custom template (using PHP-View, not Twig) when the 404 error happens?
I found this answer using Twig, but I can't figure it out how to to do it using PHP-View.
Given that you have a composer.json like that:
{
"require": {
"slim/slim": "^3.0",
"slim/php-view": "^2.2"
}
}
Here it is an example application:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use \Slim\Views\PhpRenderer;
require '../vendor/autoload.php';
$app = new \Slim\App;
$container = $app->getContainer();
$container['renderer'] = new PhpRenderer("./templates");
$container['notFoundHandler'] = function ($container) {
return function ($request, $response) use ($container) {
return $container['renderer']->render($response, "/404.php");
};
};
$app->get('/hello/{name}', function (Request $request, Response $response) {
$name = $request->getAttribute('name');
$response->getBody()->write("Hello, $name");
return $response;
});
$app->run();
And here it is the 404.php template (please notice that it is placed under the /templates subfolder as specified in app.php):
<?php
echo 'CONTENT NOT FOUND';
:)
I want to create a ¨dynamic¨ route group in Slim framework but I´m getting
Warning: Missing argument 1 for {closure}() i
this is my code:
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->group('/:segment1/:segment2', function ($segment1, $segment2) use ($app) {
$app->map('/', function () use ($app) {
})->via('GET', 'POST');
$app->map('/:id', function ($id) use ($app) {
})->via('GET', 'PUT', 'DELETE');
});
$app->run();
If i change:
$app->group('/:segment1/:segment2', function ($segment1, $segment2) use ($app)
to:
$app->group('/segment1/segment2', function () use ($app)
it starts working but I need those segments to be dynamic. how can I do that?
You have to add group parameters to their child routes function:
$app->group('/:segment1/:segment2', function () use ($app) {
$app->map('/', function ($segment1, $segment2) use ($app) {
// something
})->via('GET', 'POST');
$app->map('/:id', function ($segment1, $segment2, $id) use ($app) {
// something
})->via('GET', 'PUT', 'DELETE');
});
Also look at this issue.
I'm going to use Slim Framework. The, it seems that it is going to be everything in one single file. I don't want PHP to compile everything. How can I split them?
For example I have
$app->get('/articles/:id', function ($id) use ($app) { })
$app->get('/profiles/:id', function ($id) use ($app) { })
$app->get('/messages/:id', function ($id) use ($app) { })
$app->get('/articles', function ($id) use ($app) { })
$app->get('/search/:foo', function ($id) use ($app) { })
$app->delete('/message/:id', function ($id) use ($app) { })
Should I put include into {} so that only one file be included?
I think this can be a good solution for code comprehension. Slim is a micro-framework, so there is no real file structure, this leaves you free to do almost as you want.
You can read this article for more informations about how to organize your Slim file structure.
I take a similar approach but using group:
index.php
require '../Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
require 'routes/articles.php';
$app->run();
routes/articles.php
$app->group('/articles', function () use ($app) {
$app->get('/:foo', function($foo) use ($app) {
...
});
$app->get('/:foo/:bar', function($foo, $bar) use ($app) {
echo 'Info about all routes';
});
});