So I’m working on a Silex project that is a checkbook register. The idea is that accounts have transactions. I am using the approach of having controller providers, so I am mounting the providers. It’s clear to me that I can just mount /accounts and /transactions for the respective controller providers. Since logically transactions are children of accounts, however, I was hoping to achieve this type of URL structure:
/account/1 = get request for account ID 1
/account/1/transaction/100 = get request for transaction ID 100, including account ID 1 as a parameter
Thanks in advance.
Edit:
I failed to mention that my controller setup looks as such:
bootstrap.php:
$app->mount('/account', new AccountControllerProvider());
$app->mount('/transaction', new TransactionControllerProvider());
AccountControllerProvider.php:
$controllers->put('/', 'Mogaard\Checkbook\Controller\AccountController::createAction')
->bind('account_create');
$controllers->get('/{account}', 'Mogaard\Checkbook\Controller\AccountController::displayAction')
->convert('account', $accountProvider)
->bind('account_display');
$controllers->post('/{account}', 'Mogaard\Checkbook\Controller\AccountController::saveAction')
->convert('account', $accountProvider)
->bind('account_save');
$controllers->delete('/{account}', 'Mogaard\Checkbook\Controller\AccountController::deleteAction')
->convert('account', $accountProvider)
->bind('account_delete');
TransactionControllerProvider.php:
$controllers->put('/', 'Mogaard\Checkbook\Controller\TransactionController::createAction')
->bind('transaction_create');
$controllers->post('/{transaction}', 'Mogaard\Checkbook\Controller\TransactionController::saveAction')
->convert('transaction', $transactionProvider)
->bind('transaction_save');
$controllers->delete('/{transaction}', 'Mogaard\Checkbook\Controller\TransactionController::deleteAction')
->convert('transaction', $transactionProvider)
->bind('transaction_delete');
I am looking to add a route for /account/{account}/transaction/{transaction}, but I am unsure of how to do it using mounted controller providers and without mixing transaction controller responsibilities into the account controller provider.
As suggested by Igorw, I’ll just post this as an answer (just in case the poster didn’t read the comment):
I may not be too used to silex, but have you tried
$app->get('/account/{accId}/transaction/{transId}'...)?Edit:
Once you have that route traced, you could just forward
/account/{accId}/transaction/{transId}to/transaction/account/$accId/transaction/$transId. It will be invisible to the user, just as an.htaccesswould do. Or even use an.htaccessto map these routes to the one that fits you best.