routing-2.php

<?php

/** This file is a sample for how routes could work. It's a feature-in-progress.
 *   My final goal from this is to generate all the valid paths for this route
 * 
 */

/*

    We have three offices:
        - city-council
        - county-board
        - states-attorney

    Then for each office we have a few candidates.

    I want the system to ask this script:
    What combinatiosn are valid for 
        office, candi
    and what combinations are valid for
        office, null

    These are the two options
        office, candi
        office, null
*/

$infoList = \RDB::rawQuery("SELECT office.slug AS officeSlug, candi.slug AS candiSlug FROM office JOIN candi ON candi.office_id = office.id");

$allRoutes = [];
foreach ($infoList as $info){
    $allRoutes[$info->officeSlug] = [
        'office'=>$info->officeSlug
    ];
    $allRoutes[$info->officeSlug.'-'.$info->candiSlug] = [
        'office'=>$info->officeSlug,
        'candi'=>$info->candiSlug
    ];
}

return $allRoutes;
/*

Do these all point to the public file now?
Or do I need to specify where they point? 
Do they point to a callable?
Do they point to a particular compo?
Do they point to a view?
What if I specify routes in a class?
What if each of these rets involves a route class? Here let's add an example of that

*/

$allRoutes = [];
foreach ($infoList as $info){
    $allRoutes[$info->officeSlug] = 
        new Route(['office'=>$info->officeSlug],
        function($office){
            $this->view('office/Page',['office'=>$office])
        }
        );
    $allRoutes[$info->officeSlug.'-'.$info->candiSlug] = [
        'office'=>$info->officeSlug,
        'candi'=>$info->candiSlug
    ];
}

return $allRoutes;

/**
 * If I return a pure array, rather than wrapping in an object, then i can use that to save heavily on performance. It would enable caching & just be better in a lot of ways than creating a route object & an inline callable
 * 
 * So I want to skip the inline callable... I don't like this approach to routing. 
 * And I don't want to have two files. I DONT want to have both a route file and a public file with the same name... that just seems like extra, unnecessary code
 * 
 * So my question is:
 * How can I provide both the final routes AND the result of those routes in one file, without writing a class?
 * 
 * Perhaps there is some kind of auto-lookup mechanism I can use...
 * But if I want the routes in the same file as the results, this means I have to be able to load the standalone public file without it being requested and get the routes back instead of the results. This means a conditional, which is not what I want.
 * 
 * I could provide a query for the particular URL paramaters & on the first time the public file is requested, it could run this query to build the map of routes that lead here
 * There is a point, often times, where I have to routes that match the same url. So I need a way for the routes to say "Hey. This isn't mine. Go to the next route."
 *  and I really want all this code to exist in a single `public` file. I really like the named-public-file approach... but then how do I give routes instead of results?
 * 
 *  I could check a flag?
 *  if ($event->type=='GetRoutes'){
 *      return the routes
 *  }
 */


$sampleRoutes = [
    [
        'office'=>'city-council',
        'candi'=>null
    ],
    [
        'office'=>'county-board',
        'candi'=>null
    ],
    [
        'office'=>'states-attorney',
        'candi'=>null
    ],
    [
        'office'=>'city-council',
        'candi'=>'david-horn'
    ],
    [
        'office'=>'city-council',
        'candi'=>'marc-girdler'
    ]
    // and so on
];