CRUD Application

In this section we'll create a simple CRUD application that will demonstrate major features, if not all, of PHPasp. CRUD stands for CREATE READ UPDATE DELETE. So in this very basic example we'll be

  • Adding new User
  • Editing existing User
  • View all Users
  • Delete existing Users


First we'll tell our framework which controller method will handle the HTTP request for each of the above.

In app/routes.php add the below line

Route::add("CONTROLLER", "crud", "Crud_Controller");  

In above code we are using the CONTROLLER routing feature. Here any HTTP request that begins with will be handled by the appropriate method in Crud_Controller.

Here is how it works.

  • A GET request to /crud/add will be handled by get_add() method of controller.
  • A POST request to /crud/add will be handled by post_add() method of controller.
  • A GET request to /crud/edit/{user_id} will be handled by get_edit($user_id) method of controller.

So the method that is called is {HTTP_VERB}_{first_url_segment_following_crud}. Any url segment following the first url segment following crud will be passed as method paramter, just as {user_id} is passed to get_edit()

So CONTROLLER routing is equivalent to below

Route::add("GET", "crud/add", "Crud_Controller@get_add");
//In this case it not compulsary to have method that begins with get, you can have any method name
Route::add("POST", "crud/add", "Crud_Controller@post_add");
Route::add("POST", "crud/edit/{user_id}", "Crud_Controller@get_edit");

Using CONTROLLER routing saved us few line of code in routes.php. Ofcourse its upto you to decide which is better. The second example gives developer a better choice to decide the url or method name. While in CONTROLLER routing each method has to be prefixed with the HTTP_VERB the method responds to, in second one you can have any method (not necessarily begining with get_ or post_) of any Controller.