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.

We're using the make() method from <strong>Laravel</strong>'s Hash class to create a salted hash of the<br />

user's password.<br />

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

Step 4 – Routing to a closure<br />

Before we can move on and test our user model, we need to know a few things about routing<br />

in <strong>Laravel</strong>. Routing is the act of linking a URL to a function in your application. In <strong>Laravel</strong>, it's<br />

possible to route in two ways. You can either route to a closure or a controller action. As we'll<br />

be going over controllers in more detail later, let's start by looking at how we can route to a<br />

closuRoutes in <strong>Laravel</strong> are declared in application/routes.php. This file will represent the<br />

connection between your site's URLs and the functions that contain application logic for your<br />

site. This is very handy as other developers will be able to come into your project and know how<br />

requests are routed, simply by reviewing this file.<br />

Here is a simple example of routing to a closure:<br />

Route::get('test', function()<br />

{<br />

return "This is the test route.";<br />

});<br />

We're using the Route::get() method to define the route. Route::get() registers a<br />

closure with the router that specifically responds to a GET request at the specified URI. To<br />

register a closure for the POST, PUT, and DELETE requests, you'd use Route::post(),<br />

Route::put(), and Route::delete() respectively. These methods correspond to what<br />

are commonly referred to as the HTTP verbs.<br />

Typically, developers only interact with the GET and POST requests. When a user clicks on a link<br />

or enters a URL in their address bar, they're creating a GET request. When a user submits a form,<br />

they're typically creating a POST request.<br />

The first argument for the Route::get() method is the URI for the route (the part of the URL<br />

after the domain name), and the second argument is the closure which contains the desired<br />

application logic.<br />

Let's update the example and test the route.<br />

Notice that instead of using echo to output the string we're returning it.<br />

That's because whether you route to a closure or route to a controller<br />

action, you should always return your response. This allows <strong>Laravel</strong> to<br />

handle many situations in a robust way.<br />

Now go ahead and navigate to http://myfirst.dev/test. You will see the message, This is<br />

the test route.<br />

13

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

Saved successfully!

Ooh no, something went wrong!