11.08.2017 Views

codebright

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Controllers 85<br />

it would make sense to group this logic together. This is why the logic is contained in one single<br />

ArticleController.<br />

In honesty, you can call the Controller whatever you like. As long as your controller extends<br />

either BaseController or Controller then Laravel will know what you are trying to do. However,<br />

suffixing a controllers name with Controller, for example ArticleController is somewhat of a<br />

standard that web developers employ. If you plan to be working with others, then standards can be<br />

extremely useful.<br />

Our controller has been created in the app/controllers directory which Laravel has created for us.<br />

This directory is being ‘classmap file loaded’ from our composer.json by default. If app/controllers<br />

doesn’t suit your workflow, then you can put the file wherever you like. However, you must make<br />

sure that the class can be autoloaded by Composer. For more information on this topic, please refer<br />

back to the Composer primer.<br />

The class methods of our Controller are what actually contain our logic. Once again you can name<br />

them however you like, but personally I like to use the word ’show’ as a prefix if the result is that<br />

they display a web page. These methods must be public for Laravel to be able to route to them. You<br />

can add additional private methods to the class for abstraction, but they cannot be routed to. In fact,<br />

there’s a better place for that kind of code which we will learn about in the models chapter.<br />

Let’s have a closer look at our first action, which in this example would be used to display a blog<br />

article listing.<br />

1 public function showIndex()<br />

2 {<br />

3 return View::make('index');<br />

4 }<br />

Hmm, doesn’t that look awfully familiar? Let’s quickly compare it to a routed closure that could be<br />

used to achieve the same effect.<br />

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

2 {<br />

3 return View::make('index');<br />

4 });<br />

As you can see, the inner function is almost identical. The only difference is that the controller action<br />

has a name, and the closure is anonymous. In fact, the controller action can contain any code that<br />

the Closure can. This means that everything we have learned so far is still applicable. Shame, if it<br />

was different I could have sold another book on controllers!<br />

There is one other difference between the two snippets however. In the basic routing chapter we were<br />

taught how to route a URI to a piece of logic contained within a Closure. In the above routed closure<br />

example the URI /index would be routed to our application logic. However, our Controller action<br />

doesn’t mention a URI at all. How does Laravel know how to direct it’s routes to our controller?<br />

Let’s take a look at Controller routing and hope we find an answer to our question.

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

Saved successfully!

Ooh no, something went wrong!