Smutty
MVC Framework
ClassSmutty_Controller
Controllers are at the centre of your application, they're the place where most of your code will go.
NOTE: The "smutty" controller name is reserved and should not be used. It's advisable to not use the name Smutty at all in your application.
Creating Controllers
You can use the smut utility to easily create new controllers. This will also prepare any other information that the controller will need (like creating a new views folder for it).
./smut controller Post
This will then create the application/controllers/PostController.php file ready for use.
Actions
Defining actions in Smutty is as simple as declaring a function aith the suffix Action, like so...
class PostController extends Smutty_Controller {
function indexAction() {
// do something
}
}
This function will then handle the index (default) action for this controller. Easy.
Action Parameters
All action functions will recieve the following set of arguments when invoked.
- $data - A Smutty_Data object holding request data
- $session - The session object, contains current user as $session->user
class PostController extends Smutty_Controller {
function showAction( $data, &$session ) {
// my action...
}
}
NOTE: If you need to modify the session object then don't forget to use the & prefix to the parameter!
Redirection
To redirect the user you can simply use the redirect() function that is inherited from the Smutty_Controller class. This function takes an associative array of arguments to handle the redirection. These arguments can be anything to do with the routes you have defined, but common options are:
- controller - the name of the controller to redirect to
- action - the name of the action
All of these arguments are optional. If the controller is not specified then it will be assumed to be the current controller, the other arguments will just be left out. This function will respond to any custom routes you're using, so you can use all those arguments here to.
class MyController extends Smutty_Controller {
function saveAction() {
// save record, then redirect
$this->redirect(array(
controller => 'another',
action => 'viewAll'
));
}
}
Ajax Redirection
You can also do redirection when you're using Ajaz callbacks with the redirectJs method. This method will output the required javascript to redirect the user to the specified page. So say you've got an Ajax form callback filling a <div> on you're page for feedback, you're action may look something like this...
function saveCommentAction( $data, &$session ) {
$comment = new Comment();
$comment->fill();
// if saved ok then do a redirect
if ( $comment->save() )
$this->redirectJs(array(
action => showComment,
id => $comment->id
));
// otherwise show errors
else {
$this->set( 'errors', $comment->getErrors() );
$this->view();
}
}
Links: Post a comment