This is my index.php
<?php
$app = new \Slim\Slim(
array(
'templates.path' => dirname(__FILE__).'/templates'
)
);
// Add session cookie middle-ware. Shouldn't this create a cookie?
$app->add(new \Slim\Middleware\SessionCookie());
// Add a custom middle-ware
$app->add(new \CustomMiddleware());
$app->get(
'/',
function () use ($app) {
$app->render('Home.php');
}
);
$app->run();
?>
This is my custom middle-ware:
<?php
class CustomMiddleware extends \Slim\Middleware {
public function call() {
// This session variable should be saved
$_SESSION['test'] = 'Hello!';
$this->next->call();
}
}
?>
And this is my template (Home.php)
<?php
var_dump($_SESSION['test']);
?>
which will output NULL, so the session variable is not saved. Also, when opening the cookies list in the navigator I don't see any. Why isn't the cookie of the session saved? I verified and made sure that the call() function of the SessionCookie class is executed.
How if you add your CustomMiddleware first before Slim\Middleware\SessionCookie?
Something like this:
require 'Slim/Slim.php';
Slim\Slim::registerAutoloader();
class CustomMiddleware extends Slim\Middleware
{
public function call() {
// This session variable should be saved
$_SESSION['test'] = 'Hello!';
$this->next->call();
}
}
$app = new Slim\Slim();
$app->add(new CustomMiddleware());
$app->add(new Slim\Middleware\SessionCookie());
// GET route
$app->get('/', function () use ($app)
{
$app->render('home.php');
});
$app->run();
And your home.php template file:
<?php echo($_SESSION['test']); ?>
For me, it works flawlessly. But if I add Slim\Middleware\SessionCookie before my CustomMiddleware, the output of $_SESSION['test'] remains NULL.
This is how middleware works:
So, your response will never get any $_SESSION value, since you set your $_SESSION before SessionCookie called.
Related
I'm building a web application right now and I'm facing a problem with my controller.
I want to send to my controller my League\Plate\Engine (registred in my Container) but I keep having the same error : Argument 3 passed to App\Controller\Main::index() must be an instance of League\Plates\Engine, array given
Here is my files :
dependencies.php
use League\Container\Container;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Yajra\Pdo\Oci8;
use League\Container\ReflectionContainer;
$container = new Container();
// Active auto-wiring
$container->delegate(
new ReflectionContainer
);
// Others dependencies
// ...
// Views
$container->add('view', function () {
$templates = new League\Plates\Engine();
$templates->addFolder('web', __DIR__ . '/templates/views/');
$templates->addFolder('emails', __DIR__ . '/templates/emails/');
// Extension
//$templates->loadExtension(new League\Plates\Extension\Asset('/path/to/public'));
//$templates->loadExtension(new League\Plates\Extension\URI($_SERVER['PATH_INFO']));
return $templates;
});
return $container;
routes.php
use League\Route\RouteCollection;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
$route = new RouteCollection($container);
// Page index
$route->get('/', 'App\Controller\Main::index');
// Others routes...
return $route;
Main.php
namespace App\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use League\Plates\Engine;
class Main
{
public function index(ServerRequestInterface $request, ResponseInterface $response, Engine $templates) {
//return $response->getBody()->write($this->templates->render('web::home'));
return $response;
}
}
Thank you in advance
EDIT
I've made a progress.
I extended the Main class to extends the abstract class BaseController which looks like this :
namespace App\Controller;
use League\Plates\Engine;
class BaseController
{
protected $templates;
public function __construct(Engine $templates) {
$this->templates = $templates;
}
}
The first error goes away, but another one show up. In the Main class, I would like to use the view object that I instanciate in the container, but the object passed to the constructor is an empty one :
Main.php
class Main extends BaseController
{
public function index(ServerRequestInterface $request, ResponseInterface $response) {
echo '<pre>'.print_r($this->templates,1).'</pre>'; // Return an empty Plate Engine object
return $response->getBody()->write($this->templates->render('web::home'));
//return $response;
}
}
And this doesn't explain why the first error shows up
EDIT 2
After some digging, I finally make it works, but I sense that something is wrong.
I replaced in the container the term view by the namespace of the Engine class :
$container->add('League\Plates\Engine', function () {
// The same as before
});
In Main.php I've updated the index function like this :
public function index(ServerRequestInterface $request, ResponseInterface $response) {
$body = $response->getBody();
$body->write($this->templates->render('web::home'));
return $response->withBody($body);
}
And the page doesn't throw a 500 error and the html file is displayed correctly.
But, what if I want to change the template engine by Twig for example ? This would mean I'll need to change all the call to $container->get('League\Plate\Engine'); by $container->get('What\Ever'); ? That's not very practical!
I probably missed something!
And the problem will rise again when I'll want to use my PDO object... or every other object.
Ok so I solved my problem by registering my Controllers classes in the container itself.
For example, for displaying the index page, the Main class call the index function. In my container, I call
$container->add('App\Controller\Main')
->withArgument($container->get('view'));
To summary :
bootstap.php (called by index.php)
require __DIR__ . '/../../vendor/autoload.php';
$dotenv = new \Dotenv\Dotenv(__DIR__ . '/../');
$dotenv->load();
$config = new Config(__DIR__ . '/../config/');
$container = require __DIR__ . '/../dependencies.php';
$route = require __DIR__ . '/../routes.php';
$response = $route->dispatch($container->get('request'), $container->get('response'));
$container->get('emitter')->emit($response);
dependencies.php
$container = new Container();
// activate auto-wiring
$container->delegate(
new ReflectionContainer
);
// Others dependencies...
// Views
$container->add('view', function () {
$templates = new League\Plates\Engine();
$templates->addFolder('web', __DIR__ . '/templates/views/');
$templates->addFolder('emails', __DIR__ . '/templates/emails/');
// Extension
//$templates->loadExtension(new League\Plates\Extension\Asset('/path/to/public'));
//$templates->loadExtension(new League\Plates\Extension\URI($_SERVER['PATH_INFO']));
return $templates;
});
// THIS IS THE TRICK
$container->add('App\Controller\Main')
->withArgument($container->get('view'));
// others controllers...
return $container;
routes.php
$route = new RouteCollection($container);
// Page index
$route->get('/', 'App\Controller\Main::index');
// Others routes...
return $route;
I have no idea why or how this work !
This works, because you are registering with the container your controller class, and telling the container to dependency inject your View class in the constructor..
The line you added, here, is doing that:
$container->add('App\Controller\Main')
->withArgument($container->get('view'));
The Hello World documentation example explains it perfectly.
http://container.thephpleague.com/3.x/constructor-injection/
i am using laravel 5.3
Session not storing in controllers class method
I think the problem is with connect to api. after connect to api session not work
`
namespace App\Http\Controllers;
use App\GlosbeTranslate as Translate;
use Illuminate\Http\Request;
class translateLocal extends Controller
{
public $arrayDefine;
public function translate($word){
//connect to glosbe Api to translate from eng to fas
$glosbe = new Translate("eng", "fas");
$glosb=$glosbe->translate($word);
$result=json_decode($glosb);
//store result of glosbe api in array
foreach ($result->tuc as $key=>$value){
if(isset($value->phrase)){
$translate[]=$value->phrase->text;
}else{break;};
}
$sortedArray=$this->sort($translate);
$encodearray=json_encode($sortedArray,JSON_UNESCAPED_UNICODE);
//save in session
session(['name' => $encodearray]);
return $encodearray;
}
public function sort($translate){
usort($translate,function ($a,$b){
return strlen($a)-strlen($b);
});
return $translate;
}
}
// my web.php
Route::get('glosb/{word}', 'translateLocal#translate');
Route::get('/session', function () {
echo session('name');
});
when i load mysite.dev/session it's not show my session.
how can i solve that?
Can you give it a try?
Change
use App\putSession;
to
use Session;
You should update your code like:
use Session;
session(['name' => $encodearray]);
TO
Session::put('name',$encodearray]);
and change your route /session like:
Route::get('/session', function () {
$name = Session::get('name');
echo $name;
OR
echo session('name');
});
I'm relatively new to Slim Framework 3. One thing I'm trying to understand is how to use the router, $this->router, in a "global" template.
What I mean by this is a template such as a navigation menu - something that appears on every page.
For templates I'm using the "php-view" library as per the example tutorial which I installed with:
composer require slim/php-view
In my templates directory I have a file called nav.php where I want to output my links.
I understand how to call the router like so
Sign Up
But... the example tutorial only shows how you would pass that link from 1 individual place, e.g. $app->get('/sign-up' ... })->setName("sign-up");
How can you use the router globally in any template, without passing it into every individual URL route as a parameter?
I'm more familiar with frameworks like CakePHP where there is an "AppController" which allows you to set things globally, i.e. available in every request. I don't know if this is how it's done in Slim but this is the effect I'm after.
Well, you can pass it as template variable.
When you instantiate or register PhpRenderer in a container, you have multiple options to define a "global" variable, i.e. a variable that is accessible in all of your templates:
// via the constructor
$templateVariables = [
"router" => "Title"
];
$phpView = new PhpRenderer("./path/to/templates", $templateVariables);
// or setter
$phpView->setAttributes($templateVariables);
// or individually
$phpView->addAttribute($key, $value);
Assuming you're registering PhpRenderer via Pimple:
<?php
// Create application instance
$app = new \Slim\App();
// Get container
$container = $app->getContainer();
// Register PhpRenderer in the container
$container['view'] = function ($container) {
// Declaring "global" variables
$templateVariables = [
'router' => $container->get('router')
];
// And passing the array as second argument to the contructor
return new \Slim\Views\PhpRenderer('path/to/templates/with/trailing/slash/', $templateVariables);
};
<?php namespace App\Helpers;
/********************/
//LinksHelper.php
/********************/
use Interop\Container\ContainerInterface;
class LinksHelper
{
protected $ci;
public function __construct(ContainerInterface $container){
$this->ci = $container;
}
public function __get($property){
if ($this->ci->has($property)) {
return $this->ci->get($property);
}
}
public function pathFor($name, $data = [], $queryParams = [], $appName = 'default')
{
return $this->router->pathFor($name, $data, $queryParams);
}
public function baseUrl()
{
if (is_string($this->uri)) {
return $this->uri;
}
if (method_exists($this->uri, 'getBaseUrl')) {
return $this->uri->getBaseUrl();
}
}
public function isCurrentPath($name, $data = [])
{
return $this->router->pathFor($name, $data) === $this->uri->getPath();
}
public function setBaseUrl($baseUrl)
{
$this->uri = $baseUrl;
}
}
?>
<?php
/********************/
//dependencies.php
/********************/
$container['link'] = function ($c) {
return new \App\Helpers\LinksHelper($c);
};
// view renderer
$container['view'] = function ($c) {
$settings = $c->get('settings');
$view = new App\Views\MyPhpRenderer($settings['renderer']['template_path']);
$view->setLayout('default.php');
//$view->addAttribute('title_for_layout', $settings['title_app'] .' :: ');
$view->setAttributes([
'title_for_layout'=>$settings['title_app'] .' :: ',
'link' => $c->get('link')
]);
return $view;
};
?>
<?php
/********************/
//routes.php
/********************/
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
$app->get('/', function (Request $request, Response $response, array $args) {
return $this->view->render($response, 'your_view.php');
})->setName('home');
?>
<?php
/********************/
//your_view.php
/********************/
?>
Home
You should create a new class, e.g. MainMenu, and there you should create an array with all paths for menu. Object of MainMenu should return an array with labels and paths and then you can pass that array to your view:
$menu = (new MainMenu())->buildMenu();
$response = $this->view->render($response, "index.phtml", [
'menu' => $menu
]);
Then in your *.phtml file you have access to the $menu variable. But what if you do not want repeat that code in each route?
Use middlewares. You can pass a variable from middleware using
$request = $request->withAttribute('foo', 'bar');
and retrieve
$foo = $request->getAttribute('foo');
<?php
require 'vendor/autoload.php';
// Include all controllers
foreach(glob("controllers/*.php") as $controller)
{
include $controller;
}
// Instantiate a new Slip application
$app = new \Slim\Slim(array(
'debug' => true
));
// HOME CONTROLLER
$home = new Home;
$vr = $home->index();
// Register application routes
$app->get('/', function () {
echo $vr;
});
// Run application
$app->run();
This is my controller I want to use controllers and not keep everything in this single file. Anyhow I have a controllers map where I keep all my controllers. I automatically include them all at start however I can't seem to pass $home variable to get() method so I could call $vr indede it or $home->index()
You can pass it to your function like this:
..., function () use($home){
...
I think Slim also passes $app as the first argument to your function.
Edit: actually it doesn't according to its docs, so you'll have to pass that too inside the use statement (function arguments are URL parameters):
$app->get('/', function () use($home, $app) {
$vr = $home->index();
echo $vr;
// $app is accesible too...
});
**Route**
Route::get('admin', function()
{
return View::make('theme-admin.main');
});
**Controller**
class Admin_Controller extends Base_Controller {
public function action_index()
{
echo __FUNCTION__;
}
If I forward request to controller, then I have to define View::make in every function in controller. If I don't forward it, action function doesn't work.
Should I just forward requests to controller and use View::make inside action functions or there are better alternatives?
Actually isn't necessary to define View::make in every function of your controllers.
You can, for example, execute an action and then redirect to another action, that could View::make.
Let's say you want to create an user and then show its profile, in a RESTful way. You could do:
# POST /users
public function user_create()
{
$user = User::create(...);
// after you have created the user, redirect to its profile
return Redirect::to_action('users#show', array($user->id));
// you don't render a view here!
}
# GET /users/1
public function get_show($id)
{
return View::make('user.show');
}
you can call the controller function like this
$app = app();
$controller = $app->make('App\Http\Controllers\EntryController');
return $controller->callAction('getEntry', $parameters = array());
or you can simply dispatch the request to another controller url
$request = \Request::create(route("entryPiont"), 'POST', array()));
return \Route::dispatch($request);