xLink / CybershadeCMS

[Abandoned][Broke] Repo for CybershadeCMS
1 stars 0 forks source link

URL Generation #24

Closed xLink closed 11 years ago

xLink commented 11 years ago

We need a way to generate a URL that will work for a specific route. Ideally a method that will accept some parameters and match & generate the URL string & return it.

NoelDavies commented 11 years ago

for example?

xLink commented 11 years ago

idk something like

$objRoute->getRoute('routelabel', array('someparams'=>'topassthruhere'));

where label is the label the dev gives to it, & the params get passed thru under request or session or something

NoelDavies commented 11 years ago

I've kinda agreed to myself on using the below.

$objRoute->generateURL( 'routeLabel', array(
    'id' => 4,
    'name' => seome( 'My Label' )
));
MantisSTS commented 11 years ago

Seems like a good idea.

Where would you use it?

NoelDavies commented 11 years ago

Like this

/**
 * Show the who is online block
 *
 * @author     Daniel Noel-Davies
 * @return     Void
 */
public function showWhoIsOnline ()
{
    $objSQL = coreObj::getDBO();
    $objTPL = coreObj::getTPL();

    $users = $objSQL->queryBuilder()
                ->select('id', 'username')
                ->from('#__users')
                ->where('online', '=', '1')
                ->build();

    foreach( $users as $user )
    {
        $objTPL->assign_block_vars('wio_block.user', array(
            'username' => $user['username'],
            'url'      => $objRoute->generateURL('user_profile', array( 
                'id' => $user['id'],
                'username' => $user['username']
            ))
        ));
    }
}
MantisSTS commented 11 years ago

Yeah looks like a great idea.

I can see it coming in very handy.

CSCMS commented 11 years ago

Yeah it's going to help a lot with referencing the routes, the only real PITA I can see is the params, you might not know what params a route has off hand, so what about adding another method in to get the parameters from the route and mebe even specify which ones are required too On 23 Jan 2013 16:18, "Richard Clifford" notifications@github.com wrote:

Yeah looks like a great idea.

I can see it coming in very handy.

— Reply to this email directly or view it on GitHubhttps://github.com/cybershade/CSCMS/issues/24#issuecomment-12603161.

MantisSTS commented 11 years ago

If you can return the paramaters as keys in an array with true/false for required:

array( 'param_one' => true, 'param_two' => false, );

That way you can edit the returning array to pass it straight into the generateURL func.

Or am I just being retarded?

NoelDavies commented 11 years ago

Sounds fair, Only issue is at the moment we don't have optional arguments at the moment. they're all required.

xLink commented 11 years ago

i figured that too mantis, but then started thinking about it a bit more after and realized it would be handy to have the requirements in there too, so the array would end up liek this

array(
  'id' => array(
    'required' => true, 
    'mask' => '\\d'
  )
);
MantisSTS commented 11 years ago

Yeah Okay, well that makes sense.

NoelDavies commented 11 years ago

But when would you EVER have optional params in your url? o.0

Examples?

-- Wait, Just thought of a use-case, can you both clarify?

{
    "name": "Forum Thread",
    "pattern": "/forum/thread/:threadName-:threadID/:action",
    "requirements": {
        "threadName": {
            "mask": "[A-Za-z0-9]+",
            "required": true
        },
        "threadID": {
            "mask": "[0-9]+",
            "required": true
        },
        "action": {
            "mask": "(edit|reply|quickedit|admin)",
            "required": false
        }
    },
    "args": {
        "module": "forum",
        "method": "viewThread"
    }
}
MantisSTS commented 11 years ago

What if you then want to pass in the optional arguments? That array structure doesn't account for that in the "args" bit

xLink commented 11 years ago

it wont there, because that particular route hasnt got them in, but when you create a new route, you can add it in there anyway, so you have 5 routes setup in total,

and first one it matches, is the one that gets exec'd

NoelDavies commented 11 years ago

@DarkMantisCS what about my previous json object don't you understand? it fully supports the optional var, that var being :action. if action isn't there, it'll still link but without that last bit.

NoelDavies commented 11 years ago

Implemented. Boom :D