views:

56

answers:

1

the situation is this.

my client (who also is a programmer) asks me to develop an address book (with mysql database) with a lot of functions. then he can interact with some class methods i provide for him. kinda like an API.

the situation is that the address book application is getting bigger and bigger, and i feel like its way better to use CodeIgniter to code it with MVC.

i wonder if i can use codeigniter, then in some way give him the access to controller methods.

eg. in a controller there are some functions u can call with the web browser.

public function create_contact($information) {..}

public function delete_contact($id) {..}

public function get_contact($id) {..}

however, these are just callable from web browser. how can i let my client have access to these functions like an API?

then in his own application he can use:

$result = $address_book->create_contact($information);
if($result) {
    echo "Success";
}

$contact = $address_book->get_contact($id);

in this way, my controller methods are handling the in and out with the Models. there will be no views, cause i just have to return the data/result from the Models. and he can just use my "API" functions.

is this possible?

cause i just know how to access the controller methods with the webbrowser. and i guess its not an option for him to use header(location) to access them.

all suggestions to make this possible are welcomed! even other approaches to let me use CI to develop. perhaps there already are best practices regarding this kind of cross framework collaboration?

thanks

+3  A: 

MVC seems to have dispersed in its definition. This definition I will offer should be ideal for you.

Models are where you build your business end of the application. Operations such as create_contact, delete_contact, and get_contact belong in the model layer. The model layer is what builds your application API and should be entirely independent.

Consider the controllers purely as the user's puppeteers. Controllers accept user input - the validation, sanitation, and whatnot may be done elsewhere - and invokes the API you've already setup in the model layer. Also, controllers then specify what view to use - however complicated or simple your presentation layer is.

The presentation layer usually isn't the difficulty. As long as you are only using read operations in the view you should be fine.

To clarify, if a user wants to create a new contact, the controller may need a method called create_contact that accepts the appropriate input. However, the actual operation of creating the contact must be done in the model layer. This will allow your other developer to reuse that same operation in a completely different application by loading your model, which was already designed as an independent entity.

erisco
the problem is that the application is dealing with a database and there are a lot of classes involved. and configuration files. Doctrine files etc. its evolving to an application and not just a class he can includes an use. Its better for me to use CI to develop it, then somehow port it to him so he can use methods for handling contacts in the address book. on way to solve this i thought was the controller would act like API methods. but im not sure how to do this, how my application can integrates with his. i dont think including the index.php will make everything works for him. ideas?
never_had_a_name
Regardless of the underlying mechanics, there should be a perfect separation from what belongs in the model layer and what doesn't. Database operations are definitely part of the model layer. Configurations for the model layer should not be mixed with configurations for controllers and views. When the model layer is truly its own, nothing more than a bootstrap file will allow your other developer to use it.
erisco
+1 erisco for making me feel smart. After reading up on MCV and wonking out my own system based on that, I noticed lots of people describing models and controllers in ways that made zero sense to me. I was concerned I was doing it wrong, but your description has given me faith that I'm right on target. Thank you, this had been bothering me.
Syntax Error
While this is a fine description of code separation in MCV, is this really an accurate answer to Ajsie's question? I think Ajsie is saying his client has a non-CodeIgniter application, and he wants this app to make use of a CodeIgniter app's controllers and models.CodeIgniter is expecting to make use of the url. Simply including the index.php file or CodeIgniter file isn't going to do what you want.I suggest you set up your CodeIgniter application as a service which the non-CI app can call via cUrl or file_get_contents()
Natebot