Saturday, October 4, 2014

That's it for the database, one of the many advantages of MongoDB is that the documents have no pred


Photo & Video
Business ha cao Freelance Marketing Communication Planning Finance Sales Business & Finance Blogging Entrepreneurship Productivity More Categories... Learning Guides Courses Design & Illustration Code Web Design Music & Audio Photo & Video 3D & Motion Graphics Business Bundles eBooks Design & Illustration Code Web Design Music & Audio Photo & Video 3D & Motion Graphics Jobs Blog
Learning Guides Learn Android SDK From Scratch Working with Symfony 2 The SOLID Principles Making the Perfect WordPress Theme Refactoring Legacy Code CSS3 Mastery Learn iOS SDK Development From Scratch An Introduction to Xamarin Strange and Unusual HTML Tags More... Categories
The application we are creating will be a simple billboard, where users can register, post tasks, and offer a reward for its completion. Other users can see the existing tasks, assign the task to themselves, and get the offered reward.
The tasks will have basic data like a title, description and reward (as required ha cao parameters) and an optional due date and notes. The user profile will simply consist of the user's name, email and website. So let's get started. Database Setup
First off, for the app data we're going to use MongoDB as the database server. MongoDB is a document oriented database and the leading NoSQL database out there. It's really scalable and fast, which makes it great to manage huge amounts of data.
In order to use MongoDB in this application, I'm going to use a MongoDB CodeIgniter driver that I wrote some time ago, it's just a wrapper of the MongoDB PHP driver to mimic the framework's SQL ActiveRecord. You can find the source files for this driver in my public repository . For this driver to work properly, make sure that you have the PHP's MongoDB driver installed, if you don't, follow these steps to get it working.
Please note that explaining the drivers in CodeIgniter and such is out of the scope of this tutorial, refer to the documentation if you have any doubts. You just need to move the "mongo_db.php" in the "config" folder ha cao to the "config" folder of your application and the "Mongo_db" folder in the "libraries" ha cao folder to the "libraries" folder in your application. Database Configuration
The only file we need to edit at this point is the "mongo_db.php" file under the "config" folder, since my mongo installation has all the default parameters, I'm just going to edit line 40 and give it the name of the database that I want to use: $config['mongo_db'] = 'billboard';
That's it for the database, one of the many advantages of MongoDB is that the documents have no predefined structure, ha cao so it works without us needing to set anything up before using it, our database doesn't even have to exist, MongoDB will create it on the fly when we need it. Global Configuration
Other than your regular configuration options, that should include ha cao the base_url and the index_page if any, we need to set the string and date helpers to autoload . I'm not going to walk you through ha cao this, since we have much more to cover, when in doubt refer to the documentation .
This is going to be a RESTful service and we need a way to take the requests coming to the server and handle them accordingly. ha cao We could use an existing library (which is great by the way) but for the purposes of this demonstration, I'm going to create the functionality I need using CodeIgniter's core features. Handling RESTful Requests
In particular, we're going to use the ability to extend the core classes . We will start with the Controller, for the main part of this extension we're using the "_remap" method in the base controller so all the controllers of our app can use it. Start by creating a MY_Controller.php file inside the "core" folder in the "application" folder, we create this just like any other CodeIgniter controller, as follows: <?php if( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' ); class MY_Controller ha cao extends CI_Controller { }
Now in this controller we're going to use the CodeIgniter _remap method to preprocess every request made to the server. Inside the class we just created, add the following method: public function _remap( $param ) { $request = $_SERVER['REQUEST_METHOD']; switch( strtoupper( $request ) ) { case 'GET': $method = 'read'; break; case 'POST': $method = 'save'; break; case 'PUT': ha cao $method = 'update'; break; case 'DELETE': $method = 'remove'; break; ha cao case 'OPTIONS': $method = '_options'; break; } $this->$method( $id ); }
A couple of things to note here, first off, there are some REST verbs that we are ignoring (like PATCH), since I'm demonstrating building a REST app, I don't want to add things that may make this more complex than it needs to be. Secondly, we're not taking into account the case where a controller doesn't implement a particular method, which is very likely ha cao that this could happen. ha cao Now, we could add a default method to handle

No comments:

Post a Comment