21.11.2014 Views

Laravel Starter - PHP User Group (Myanmar)

Laravel Starter - PHP User Group (Myanmar)

Laravel Starter - PHP User Group (Myanmar)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Laravel</strong> <strong>Starter</strong><br />

When an action is prefixed with get_, it will only respond to the GET requests. An action<br />

prefixed with post_ will only respond to the POST requests. The same is true of put_ and<br />

delete_. This gives us more code separation and allows us to really improve the readability<br />

and maintainability of our application.<br />

Step 11 – Creating a form for adding users<br />

It's time to give our site's administrators the ability to create users. We're going to need a new<br />

form. Let's start off by creating the file application/views/users/create.php and<br />

populating it with the following form HTML:<br />

Create a <strong>User</strong><br />

<br />

Real Name: <br />

Email: <br />

Password: <br />

<br />

<br />

Then, let's create a controller action for it. In our <strong>User</strong>s_Controller, let's add the<br />

following action.<br />

public function get_create()<br />

{<br />

return View::make('users.create');<br />

}<br />

Now, we can go to http://myfirst.dev/users/create and see our form. Once again,<br />

it's not pretty but sometimes simple is best.<br />

Step 12 – Routing POST requests to a controller action<br />

When we submit the form it's going to make a POST request to our application. We haven't yet<br />

created any actions to handle the POST requests, so if we submit it now we're going to get a 404<br />

error. Let's go ahead and create a new action in <strong>User</strong>s_Controller:<br />

public function post_create()<br />

{<br />

return "The form has been posted here.";<br />

}<br />

Notice that this method has the same action name as the get_create() method that we're<br />

using to show the create user form. Only the prefix is different. The get_create() method<br />

responds to the GET requests where the post_create() method responds to the POST<br />

requests. In the case of our create user form, the post_create() method receives the<br />

contents of the form's submitted input fields.<br />

21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!