Setting up user login session code in MVC.

I am NOT getting my populated array results printed to page.

using ...

controllers/sessions.php

<?php

  include(MODEL_PATH.'user.php');


switch ($route['view']) {

  case "new":

  break;

  case "create":
 echo OK; 

  print_r($params['user']);

  break;

}

?>

the page i am routing to (from sessions/new to sessions/create)
http://localhost/localFixers/sessions/create

echos ...
OK
for

  case "create":
 echo OK; 

yet prints nothing for

print_r($params['user']);

I Did a var dump which shows the [user] Array is being populated.

index.php

<?php

  // starts a session
  	session_start();

  // Holds the configuration for app, CONSTANTS ect...
	// Good idea to look inside.
	// * change the directory of your app in here
	// * edit your routes in here.
	// * edit database params in here.
	include('config.php');

die('<pre>'.print_r(get_defined_vars(), true).'</pre>');


  function dispatcher($routes)

AND SO ON for code

Array
(
[GLOBALS] => Array
(
[GLOBALS] => Array
RECURSION
[_POST] => Array
(
[user] => Array
(
[username] => User1
[password] => password
)

            )

How do I TEST/DEBUG code to see why the populated Array
is not displaying my array results on the page I have routes to?

sessions/create

should display

Array ([username] => User1 [password] => password )

    Hi anakadote

    Thanks for the reply!

    php code is printing out fine when finding posts $routes array
    http://localhost:8080/localFixers/posts

    but not when finding sessions $routes

    I am defining $params in index.php

    <?php
    
       // starts a session
    	 session_start();
    
    /**
    	 * dispatches route
    	 * @param array $routes
    	 * @return array
    	 */
    	include('config.php');
    
      function dispatcher($routes)
      {
         // Requested URL
    	  $url = $_SERVER['REQUEST_URI'];
    
      // Removes Apllication root from url
      $url = str_replace('/'.APP_ROOT.'/', '', $url);
    
    	// holds the named captures, $_POST data
      $params = parse_params();
    
    	// Removes query string from $url we don't need it anymore affect routes.
    	$url = str_replace('?'.$_SERVER['QUERY_STRING'], '', $url);
    
    	//  becomes true if $route['url'] matches $url
      $route_match = false;	
    
    	// loops over $routes looking for a match
    	foreach($routes as $urls => $route)
      {
    		// if match found appends $matches to $params
    		// sets $route_match to true and also exits loop.
    	  if(preg_match($route['url'], $url, $matches))
    	  {
    		  $params = array_merge($params, $matches);
    		  $route_match = true;
    	    	break;
    	  }
      }
    
    	// if no route matched display error
    	if(!$route_match) { exit('no route found'); }
    
    	// include controller
      include(CONTROLLER_PATH.$route['controller'].'.php');
    
    	if(file_exists(VIEW_PATH.'layouts'.DS.$route['controller'].'.php'))
    	{
    			// includes controller layout
    	    include(VIEW_PATH.'layouts'.DS.$route['controller'].'.php');
    	}
    	else
    	{
    		// include default layout
    	  include(VIEW_PATH.'layouts'.DS.'application.php');
    	}
    
    	// reset our flashs back to empty.
      $_SESSION['flash']['notice'] = '';
    	$_SESSION['flash']['warning'] = '';
    
      }
    
    dispatcher($routes);
    
    /**
     * Return array of $_GET and $_POST data
     * @return array
     */
    function parse_params()
    {
      $params = array();
    
    	if (!empty($_POST))
    	{
    	  $params = array_merge($params, $_POST);
    	}
    
      if (!empty($_GET))
    	{
    	  $params = array_merge($params, $_GET);
    	}
    
    	return $params; 
    }
    
    ?>
    
    
    

    I am defining $routes in config.php

    
    <?php
    
    /**
     * Holds the configuration for app, CONSTANTS ect...
     * Good idea to look inside.
     * change the directory of your app in here
     * edit your routes in here.
     */
    
    
       // routes url to controller and view
    
    $routes = array(   
    				   // posts
    	               array('url' => '/^posts\/(?P<id>\d+)$/', 'controller' => 'posts', 'view' => 'show'),
    				   array('url' => '/^posts\/new$/', 'controller' => 'posts', 'view' => 'new'),
    				   array('url' => '/^posts\/create$/', 'controller' => 'posts', 'view' => 'create'),
    				   array('url' => '/^posts\/(?P<id>\d+)\/edit$/', 'controller' => 'posts', 'view' => 'edit'),
    				   array('url' => '/^posts\/(?P<id>\d+)\/update$/', 'controller' => 'posts', 'view' => 'update'),
    				   array('url' => '/^posts\/(?P<id>\d+)\/delete$/', 'controller' => 'posts', 'view' => 'delete'),
    
    
    				 	// homepage
    	               array('url' => '/^(posts)?\/?(page\/)?(?P<page>\d?)$/', 'controller' => 'posts', 'view' => 'index'),
    
    					// sessions
    					array('url' => '/^sessions\/new$/', 'controller' => 'sessions', 'view' => 'new'),
    					array('url' => '/^sessions\/create$/', 'controller' => 'sessions', 'view' => 'create'),
    					array('url' => '/^sessions\/delete$/', 'controller' => 'sessions', 'view' => 'delete')
    
    
    
    				);
    
    
    
    // The server root
    define('SERVER_ROOT', $_SERVER['DOCUMENT_ROOT']);	
    //echo $_SERVER['DOCUMENT_ROOT'].'<br />';
        // Directory structures
    	define('DS', '/');
    
    // Application Directory - *** change this to your app folder. *** case sensative
    define('APP_ROOT', 'localFixers');	
    //echo APP_ROOT;
        //address of website	*** change to http://localhost for virtual server ***
    	define('WEBSITE', 'http://localhost:8080/');
    
    // MVC paths
    define('CONTROLLER_PATH', SERVER_ROOT.DS.APP_ROOT.DS.'controllers'.DS);
    define('MODEL_PATH', SERVER_ROOT.DS.APP_ROOT.DS.'models'.DS);
    define('VIEW_PATH', SERVER_ROOT.DS.APP_ROOT.DS.'views'.DS);
    
    //echo CONTROLLER_PATH.'<br />';
    //echo MODEL_PATH.'<br />';
    //echo VIEW_PATH.'<br />';
    
    
    //lib includes ...extends what files can do and shares with multiple applications
    include ('lib/database.php');
    include ('lib/controller.php');
    include ('lib/model.php');
    include ('lib/view.php');
    ?>
    
    

      It's a lot to look over, but I don't see where $params['user'] is being set anywhere. What do you get when you try print_r($params) instead of this print_r($params['user']) ?

        well that prints

        Array ( [0] => sessions/create )

          RESOLVED

          Hey anakadote

          I replaced my index.php with the index.php I am using on my server and still had the same problem so I did a copy and paste of the text using note pad.

          NOW using ...

            case "create":
          
            //print_r($params['user']);
          
            print_r($params);
            break;
          

          I get what I need.

          Array ( [user] => Array ( [username] => User1 [password] => password ) [0] => sessions/create )

          I didn't see any difference in the index.php files yet Problem Solved.

          Thanks for your assistance!

          Oh and I found the TEST/DEBUG code I was asking about to test my code routes in case anyone needs it.

          I placed it in my index file like so ...

          		// if no route matched display error
          		if(!$route_match) { exit('no route found'); }
          print('controller: '.$route['controller'].' view: '.$route['view'].' id: '.$params['id']).'<br />';
          

          which gives

          controller: sessions view: create id:

          ' id: '.$params['id'] is blank right now because I am not connecting to the database yet to get
          user_id = '%s'

          cheers

            Write a Reply...