How to handle Session in slim so that every time user access some pages without login it redirect to the login page


#1

Hi All,
I wanna know how can i handle pages that they cannot be accessed without login. For this purpose i have study session but i need some explanation how i will implement it in slim framework. Any help in this regard will be appreciated. Thanks


#2

You would typically call session_start() in your index.php, then write a middleware that verifies the user is logged in. Routes you want protected would have that middleware applied. There are quite a few topics on that here in this forum, and I’ve built a little Slim Auth Demo App to show people here previously that might be helpful.


#3

Dear Sir
I have found error:
Notice: Trying to get property of non-object in C:\xampp\htdocs\Myapp\app\Middleware\ValidationErrorsMiddleware.php on line 18

Fatal error: Call to a member function getEnvironment() on null in C:\xampp\htdocs\Myapp\app\Middleware\ValidationErrorsMiddleware.php on line 18

when I have use:
$this->container->view->getEnvironment()->addGlobal(‘errors’, $_SESSION[‘errors’]);
in ValidationErrorMiddleware.php as:

<?php namespace App\Middleware; class ValidationErrorsMiddleware extends Middleware { public function __invoke($request, $response, $next) { //var_dump($_SESSION['errors']); $this->container->view->getEnvironment()->addGlobal('errors', $_SESSION['errors']); unset($_SESSION['errors']); $response = $next($request, $response); return $response; } } Validation .php is :protected $errors; public function validate($request, array $rules) { foreach ($rules as $fieldName => $value) { try { $value->setName(ucfirst($fieldName))->assert($request->getParam($fieldName)); }catch (NestedValidationException $e){ $this->errors[$fieldName] = $e->getMessages(); } } $_SESSION['errors'] = $this->errors; return $this; } app.php is : $app->add(new \App\Middleware\ValidationErrorsMiddleware($container)); What is the main problem ?

#4

Does that middleware have a constructor that accepts the container you are passing?


#5

This is middleware:

<?php namespace App\Middleware; use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; class Middleware { protected $container; public function __constructor($container) { $this->container = $container; } } This is app: $app->add(new \App\Middleware\ValidationErrorsMiddleware($container)); but where is problem ? I could not found.

#6

If you var_dump the view object, is it what you would have expected?


#7

dear tflight.
I have used jquery form validation plugin in slim. I have Master form id mainForm. It has many sections , I want to use hide and show trigger to show single section1 and others are hide with in mainForm. It works well for section1 but when a button is clicked Section1 validation works but not switched to section2 . In case of I remove validation rules for section2 it works well . What is the solution ?

html is :

   <div class="navbar" id="nav">

		<?php  include'/formPartials/nav.phtml'; ?>
		
	</div>	

	<div class="page" id="page_1">

		<?php include'/formPartials/form01.phtml'; ?>
		
	</div>

	<div class="page" id="page_2">

		<?php include'/formPartials/form02.phtml'; ?>

	</div>

	<div class="page" id="page_3">

	<?php  include'/formPartials/form03.phtml'; ?>

send

form validation:

$(document).ready(function(){

$("#mainform").validate({

    ignore: "input[type='text']:hidden",
      
    rules: {
        
       text1: {
            required: true,
            
                               
        },
       text2: {
            required: true,
            
           
        },
       text3: {
            required: true,
            
           
        },


page switching.js

var currentPage = 1;

$(function () {
$().PageSwitch(‘page’, currentPage);

$("#next").click(function(){

   if($("#mainForm").valid()){
        
       currentPage++;
        $().PageSwitch('page', currentPage)
    }

});

$("#prev").click(function(){

    currentPage--;

    $().PageSwitch('page', currentPage)

});

});


#8

I don’t know, sorry. I don’t do client side validation, only server side.


#9

Thank you very much for your remarks and guide. Now I have found difficulties to make multilingual page in Slim Framework. Do you have great idea or templates for making multilingual page. Thanks in advance.


#10

You could take a look at this skeleton project for Slim 3. It has support for translations (server- and client side)