资源说明:Todos App tutorial for swpMVC framework
##Rendering the Template
Before we can easily access our templates, we need to set the view folder for our controller.
Define a \_\_construct method for the TodosController as follows:
_templatedir = dirname(__FILE__).'/../templates/';
parent::__construct();
}
We can now render the template from our controller method. Replace the "more to come" notice in the TodosController
edit_todos_list method with the code below:
form_helper()->_prefix = 'new_todo';
new TodoControlRenderer($new_todo);
get_header();
echo $this->template('edit_todos')->replace('new_todo_form',
$new_todo->render($this->template('edit_todos')->copy('new_todo_form'))
);
get_footer();
}
Reload /mytodos in your browser, and you should see something a bit more interesting,
although it probably could use some styling.
Create an assets subdirectory in your plugins folder, then a css folder in the assets folder, and in there place a style.css file with
the below contents:
.btn-primary {
background-color:
#2B54FB;
}
.form-actions, .existing_todo {
background-color:
#ECECEC;
margin-top: 18px;
margin-bottom: 18px;
width: 100%;
}
.existing_todo {
display:block;
overflow:hidden;
}
.existing_todo .completed {
float:right;
}
.existing_todo_desc_input {
display:none;
}
.existing_todo.completed_1 {
text-decoration: line-through;
}
.todos_user span {
font-style: italic;
}
Let's assume we want to include our stylesheet on all of the routes handled by this controller. The easy way to do this
is using the controller
[before method](http://streetwise-media.github.com/Streetwise-Wordpress-MVC/#controllers/public-function-before)
and the controller
[_styles property](http://streetwise-media.github.com/Streetwise-Wordpress-MVC/#controllers/this-_styles).
Create the following before method to enqueue the stylesheet on all routes handled by the TodosController, adjusting the
url to match your plugin folder structure:
_styles = array(
array('todos_styles', '/wp-content/plugins/swp_todos/assets/css/style.css')
);
}
Reload the /mytodos page again and you should see the styles applied to the form.
##Processing form submission
We've pointed our form to /todo/create, so we should probably point a route there and create a controller method to
handle it. Once we're done, we'll refactor the way we define this in the view to prevent us having to update references
to the route if we decide to change that url later.
First add the following to the add_routes method of your swpMVC_Todos class:
'TodosController', 'method' => 'show_todos_list',
'route' => '/todos/:p'
);
$routes[] = array(
'controller' => 'TodosController', 'method' => 'edit_todos_list',
'route' => 'mytodos'
);
$routes[] = array(
'controller' => 'TodosController', 'method' => 'create_todo',
'route' => '/todo/create'
);
return $routes;
}
Now we need to create the controller method we've pointed that route to. Add the following to your TodosController:
user_id = get_current_user_id();
$new_todo->save();
return header('Location: '.$redirect);
}
With this in place, you can reload your /mytodos page, and submit a few times. We're not showing existing todo items yet,
but if you look at the wp_swpmvc_todos table in your database, you should see the todo items being saved.
Note the [link method](http://streetwise-media.github.com/Streetwise-Wordpress-MVC/#controllers/self-link)
used to generate the redirect. By creating our URLs this way, we will not need to update the references to these routes if
we update the URLs associated with those methods in our plugin routes. Let's go back and refactor the hard coded target URL
in the new todos form.
In your edit_todos.tpl view file, change the form tag to the following:
#swpMVC Todos To get familiar with the swpMVC framework as quickly as possible, we'll walk through the obligatory Todo App example. We'll leverage as much of the framework as is reasonable, and upon completion we'll aim to have the following pages in our app: * The /todos url will show a listing of all users with todo lists, and each listing links to that users todo lists * The /mytodos url will show the todo list of the currently logged in user, or redirect to login page if logged out. When viewing your own todo list, you will be able to add and edit items on your list. * The /todos/{{user\_nicename}} url will show a specific users todo list. When viewing another users todo list, it will be shown in a read only mode ##Preparing the main plugin file While swpMVC does not feature code generation, there is a template plugin included in the framework to get us started, so we first copy the contents of the swpmvc/starter_plugin folder to a new directory where our todos plugin will live. Once those files have been copied over, open up plugin.php in your editor, and make the following changes: * Edit the plugin header comments at top to contain your info. * Change the plugin class name, and references to it on lines 12, 26, and 47 from swpMVC\_Starter to swpMVC\_Todos ##Creating a controller Before we fill out the add\_routes method in our plugin method, we should create a controller for our app that will handle those routes, and require it in the require\_dependencies method of the plugin class. Create a new file in the controllers subfolder of your plugin directory called TodosController.php and add the following code: 'TodosController', 'method' => 'show_todos_list', 'route' => '/todos/:p' ); return $routes; } Now in the TodosController class on line 5 of TodosController.php, let's add the todos_list method that will handle this request, with some dummy code to make sure it works. set404(); get_header(); echo 'Hello '.$user_nicename.'!'; get_footer(); } Right now our method simply checks that a parameter has been passed (defined in our route definition by the ":p" tag at the end of the route.) If not, it returns a 404 page provided by our WordPress theme, and if one is provided, will say hello using the parameter provided. To test this out, make sure the Todos plugin is active, and go to the /todos/developer url on our site. You should see a very underwhelming "Hello developer!" message there. Try a few other routes, like /todos/dawg, /todos/fool, etc, to see the dynamic goodness, and when you're bored of that, let's move on to more interesting stuff. ##First Real Route For our app to do anything (no pun,) we need to get some data into the database. Let's get the personal todos interface working so we can save a list and start seeing things work. First add the route to the add\_routes method in your plugin class as follows: 'TodosController', 'method' => 'show_todos_list', 'route' => '/todos/:p' ); $routes[] = array( 'controller' => 'TodosController', 'method' => 'edit_todos_list', 'route' => '/mytodos' ); return $routes; } And now let's add the edit\_todos\_list method to the controller: prefix . "swpmvc_todos"; $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, user_id mediumint(9) NOT NULL, description text NOT NULL, completed tinyint(1) NOT NULL, UNIQUE KEY id (id) ) CHARACTER SET utf8 COLLATE utf8_general_ci;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } And then add it to the plugin activation hook at the bottom of plugin.php as follows: 'Todo', 'foreign_key' => 'user_id'); } Now add a call to that method in the plugin class constructor, right after require\_dependencies is called: require_dependencies(); $this->update_relationships(); $this->add_actions(); } Now that we've told ActiveRecord that our User has many todos, which are modeled by the Todo class, let's create the Todo class, which will model Todos based on the table our plugin has created. First create a file called Todo.php in the models subdirectory of the plugin folder, and require it from the require\_dependencies of the plugin class as follows: private function require_dependencies() { require_once(dirname(__FILE__).'/models/Todo.php'); require_once(dirname(__FILE__).'/controllers/TodosController.php'); } Add the following code to your Todo.php: prefix.'swpmvc_todos'; } public static $belongs_to = array( array('user', 'class' => 'User', 'foreign_key' => 'user_id') ); } We've now told ActiveRecord that our Todos model is based off of the wp_swpmvc_todos table, (with a multisite aware prefix,) and that it belongs to a property called user, which is modeled using the User class, and linked up by the value of the user_id property of a Todos model instance. ##Defining Model controls with a ControlRenderer We'll want to generate our form controls programatically and make sure that code is reusable, so let's [define a ControlRenderer ](http://streetwise-media.github.com/Streetwise-Wordpress-MVC/#model-extenders/controlrenderers) for the Todo model. First create a folder called control\_renderers in your plugin folder, and there add a file called TodoControlRenderer.php, then require it from the plugin class require\_dependencies method as follows: private function require_dependencies() { require_once(dirname(__FILE__).'/models/Todo.php'); require_once(dirname(__FILE__).'/controllers/TodosController.php'); require_once(dirname(__FILE__).'/control_renderers/TodoControlRenderer.php'); } Now we will define the TodoControlRenderer class as follows:Add a Todo