Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cake 3.x How to use database sessions with a controller named SessionsController?

Cake Version 3.1.2

What I did:

  1. Run the following query in MySQL database:

    CREATE TABLE `sessions` (
      `id` varchar(255) NOT NULL DEFAULT '',
      `data` BLOB, -- or BYTEA for PostgreSQL
      `expires` int(11) DEFAULT NULL,
       PRIMARY KEY (`id`)
    );
    
  2. Change app.php

    'Session' => [
        'defaults' => 'database',
    ],
    

What I wanted:

To have a SessionsController.php

so I can have :

  • /sessions/add as the login page and action (allowing GET and POST)
  • /sessions/delete as the logout page and action (allowing GET and DELETE)
  • /admin/sessions/delete as a way for admin backend to logout certain users (allowing DELETE)
  • /admin/sessions/index as a way to fetch all the sessions in a paginated way (allowing GET)

Questions in my mind:

  1. Do I actually bake Session Entity and Table?
  2. Do I actually bake Session Controller?
  3. Is it even possible for me to have SessionsController even when I don't use database to handle my Session? How do I bake such a Controller that does not have a default Entity or Table?
like image 978
Kim Stacks Avatar asked Nov 26 '15 05:11

Kim Stacks


1 Answers

Most of the questions you have are answered in the CakePHP Book. The answers to your questions are:

  1. Do I actually bake Session Entity and Table?
  2. Do I actually bake Session Controller?

    A: No, you don't HAVE to bake anything. You can write all the code by hand. Although, I recommend baking to set up the skeleton of the classes.

  3. Is it even possible for me to have SessionsController even when I don't use database to handle my Session?

    A: Yes, you can name your controllers anything you want as long as you follow the naming conventions.

  4. How do I bake such a Controller that does not have a default Entity or Table?

    A: You just bake it. You don't have to use models, default or otherwise, in your controllers.

like image 115
chrisShick Avatar answered Oct 03 '22 01:10

chrisShick