Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Why is "Index.php?action=" not recognized as "Index.php?"

All,

I'm building a front controller in PHP. In it, I say:

if (isset($_GET['action'])){
        $action=$_GET['action'];
    } else {
        $action='';
    }

I follow that with a switch statement than governs which controller is called based on the value of $action:

switch ($action){
        case '':
            require_once('Controller_Welcome.php');
            $command=new controller_Welcome();
            break;
        case 'logon':
            require_once('Controller_Logon.php');
            $command=new controller_Logon();
            break;
        default:
            require_once('Controller_Unknown.php');
            $command=new controller_Unknown();
            break;
    }
$command->execute();

This works fine. When I launch the app, the URL is http://.../Index.php? and the Controller_Welcome.php is called. If I click on the logon menu entry, I get http://.../Index.php?action=logon, and the Controller_Logon.php is called. If I manually edit the URL to set ...?action=... to some unknown value, I get Controller_Unknown.php, which is my error page. So all is well.

What I don't understand is that if I manually alter the url to show http://.../Index.php?action=, I get the error page rather than the welcome page. Why is it that php doesn't associate a url ending with ...?action= with the switch case $action='';?

(There's no logical user case when that would happen, but I still don't understand it...)

Thanks,

JDelage

PS: Var_dumping $action returns string(0) "".

like image 844
JDelage Avatar asked Jul 27 '26 23:07

JDelage


1 Answers

Just a note that may help with readability, and further development efforts. It appears your naming convention could permit a bit more "magic", giving way for a sort of convention over configuration, and avoidance of code duplication:

define('PATH_CONTROLLERS', 'path/to/controllers/');

$action = !empty($_GET['action'])
    ? $_GET['action']
    : 'default';

switch($action){
    case 'default':
    case 'welcome':
    case 'authenticate':
        $controller_name = "controller_{$action}";
        break;
    default:
        $controller_name = 'controller_404';
        break;
}

require PATH_CONTROLLERS . "{$controller_name}.php";
$controller = new $controller_name();

$controller->execute();

Given:

// index.php

$action:          'default'
$controller_name: 'controller_default'
require():        'path/to/controllers/controller_default.php'

// index.php?action=authenticate

$action:          'authenticate'
$controller_name: 'controller_authenticate'
require():        'path/to/controllers/controller_authenticate.php'

// index.php?action=foobar

$action:          'foobar'
$controller_name: 'controller_404'
require():        'path/to/controllers/controller_404.php'
like image 125
Dan Lugg Avatar answered Jul 30 '26 19:07

Dan Lugg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!