Accessing Controller functions from single page in a Package
Permalink
Hello everyone --
I have a package I'm using to store info about dependents for each user. It's working great, thanks to Jeremy Werst's package demo.
The issue I have right now is that I would like to have a single page in the package, where any user can add a new dependent. I have a small form set up, but I'm not sure where I should link the action to. My gut says to link it to a function in the package controller, but I'm not sure how to do that.
As it stands, my single page is:
and my package controller is, in part:
but this doesn't work -- I get a 404 Page not found error.
Anybody have any advice or tips on how to do this? I've looked in various packages, but they're either too sparse or waaaay too complex for me to understand.
Thanks!
I have a package I'm using to store info about dependents for each user. It's working great, thanks to Jeremy Werst's package demo.
The issue I have right now is that I would like to have a single page in the package, where any user can add a new dependent. I have a small form set up, but I'm not sure where I should link the action to. My gut says to link it to a function in the package controller, but I'm not sure how to do that.
As it stands, my single page is:
<form method="post" action="<?php echo $this->url($pkg->getRelativePath(), 'do_register')?>" class="form-horizontal"> <?php print $form->label('firstName', 'First Name'); print $form->text('firstName', "First Name"); print $form->label('lastName', 'Last Name'); print $form->text('lastName', "Last Name"); print $form->label('legalName', 'Full Legal Name'); print $form->text('legalName', "Full Legal Name"); print $form->submit('submit', 'Submit Registration'); ?>
and my package controller is, in part:
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); class StudentInfoPackage extends Package { protected $pkgHandle = 'student_info'; protected $appVersionRequired = '5.5.2'; protected $pkgVersion = '0.1.14'; protected $singlePages = array( '/add_student' ); public function do_register() { $firstName = $_POST['$firstName']; $lastName = $_POST['$lastName']; $legalName = $_POST['$legalName']; $si = StudentInfo::add(array($firstName, $lastName, $legalName)); if ($si) {
Viewing 15 lines of 19 lines. View entire code block.
but this doesn't work -- I get a 404 Page not found error.
Anybody have any advice or tips on how to do this? I've looked in various packages, but they're either too sparse or waaaay too complex for me to understand.
Thanks!
NetJunky -- yes, the controller is in the package. That's the problem I'm having -- it can't seem to find the controller, or it can't find the function in the controller.
The controller for a single page inside of a package follows a path similar to the single page. For instance, if the single page is in [package root]/single_pages/students/student_info.php then the controller is located in [package root]/controllers/students/student_info.php
To submit a form to a single page controller, the form action should be the page url plus the name of the function to be called when the form is submitted. For instance, if you wish to call the addStudent function, the form action would be
http://site.com/students/student-info/addStudent....
To submit a form to a single page controller, the form action should be the page url plus the name of the function to be called when the form is submitted. For instance, if you wish to call the addStudent function, the form action would be
http://site.com/students/student-info/addStudent....
I created a package with a single page like this and I stored the page id in the config table in the database so that I could easily access it.