PHP on T R A X
Rapid Application Development Made Easy

Reply to topic
Id as root argument
qistoph_


Joined: 01 Oct 2007
Posts: 3
Reply with quote
Hi all,

For a few days I've been using PHP on Trax just to investigate it's possibilities and acquire some skills in using it.

I was wondering if it would be possible to pass the id argument as first 'parameter' in the url.

Say I'm running my PHP on Trax application on http://trax.localhost/trax
That would make my 'main' controller available at http://trax.localhost/trax/main
When I want to give an id as parameter I'd use http://trax.localhost/trax/main/index/1

Now, what I want is to be able to have a url like this:
http://trax.localhost/trax/1
So I won't have to enter the controller, nor the action.

Is it possible to accomplish this with PHP on Trax?

Kind Regards,
qistoph
View user's profileSend private message
johnpipi


Joined: 15 Dec 2005
Posts: 328
Reply with quote
Yes you can do this easily:

in the config/routes.php file:
Code:

$router->connect(":id", array(":controller" => "main"));
View user's profileSend private message
qistoph_


Joined: 01 Oct 2007
Posts: 3
Reply with quote
Dear johnpipi,

I've tried your suggestion (with Trax from PEAR and with Trax from SVN) and all I get is:
Controller 1 not found

Error Message: Failed to load any defined routes

With in the log-file:
Code:
[03-Oct-2007 18:49:34] url=/1
[03-Oct-2007 18:49:34] route regexp=/^[a-z0-9_\-]+$/
[03-Oct-2007 18:49:34] route regexp=/^$/
[03-Oct-2007 18:49:34] route regexp=/^[a-z0-9_\-]+\/[a-z0-9_\-]+\/[a-z0-9_\-]+$/
[03-Oct-2007 18:49:34] defaulted
[03-Oct-2007 18:49:34] selected route=array (
  'path' => ':controller/:action/:id',
  'params' => NULL,
)


If I change the line, this is what I get:
$router->connect("/:id", array(":controller" => "main"));
Loads main/index, but without any id argument.
Code:
[03-Oct-2007 18:50:20] url=/1
[03-Oct-2007 18:50:20] route regexp=/^\/[a-z0-9_\-]+$/
[03-Oct-2007 18:50:20] selected
[03-Oct-2007 18:50:20] selected route=array (
  'path' => '/:id',
  'params' =>
  array (
    ':controller' => 'main',
  ),
)


$router->connect("/:id", array(":controller" => "main", ":action" => "index", ":id" => 2));
Loads main/index, but without any id argument.
Code:
[03-Oct-2007 18:51:12] url=/1
[03-Oct-2007 18:51:12] route regexp=/^\/[a-z0-9_\-]+$/
[03-Oct-2007 18:51:12] selected
[03-Oct-2007 18:51:12] selected route=array (
  'path' => '/:id',
  'params' =>
  array (
    ':controller' => 'main',
    ':action' => 'index',
    ':id' => 2,
  ),
)


I hope you've got another suggestion.

Kind Regards,
qistoph
View user's profileSend private message
johnpipi


Joined: 15 Dec 2005
Posts: 328
Reply with quote
I did a test and in my routes.php I have these
Code:

$router->connect(":id", array(":controller" => "main") );
$router->connect( ":controller/:action/:id" );


This worked for me id was set. but it sets the id in the request so $_REQUEST['id'] is how you access it in the controller.

The only problem if you had a route like that with only :id it basically overrides everything else and thinks everything is this route. So what you should do is make up a name that no other controller has /:id and use that such as:

$router->connect("pictures/:id", array(":controller" => "pictures", ":action" => "show") );

If you went to the url http://www.phpontrax.com/pictures/24
This would load the pictures controller, the show action, and $_REQUEST['id'] would be 24

Hope this helps.
View user's profileSend private message
qistoph_


Joined: 01 Oct 2007
Posts: 3
Reply with quote
Thanks johnpipi.

With a little more debugging I discovered the problem was in my 'url_prefix'.
In config/environment.php an example is given in this format:
Trax::$url_prefix = "~username";

When I added a trailing slash to the Trax::$url_prefix the routes worked fine.

Thanks for your assistence.
View user's profileSend private message
Id as root argument
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 7 Hours  
Page 1 of 1  

  
  
 Reply to topic