No ratings yet.

what are restful controllers in laravel framework

 

 

 

 

 

 

 

 

RESTful Controllers

Laravel offers solutions, we know this much. It also provides options, RESTful Controllers are a
prime example of this.

Laravel keeps in its arsenal the tool of restful controllers.
We have read in the previous chapter about responses -what they are and how we can use them.
Now we focus on restful controllers.we can define the HTTP request verb that we want to match using the routing
methods.

<?php

// app/controllers/Mature.php

namespace MatureClass\Controller;
class Mature
{
public function getCreate()
{
return View::make(‘create’);
}

public function postCreate()
{
// Handle the creation form.
}
}
Here we have our Article controller once more. The intention of the actions are to provide a form
to create and handle the creation of a new blog article. You’ll notice that the names of the actions
have been prefixed with get and post. Those are our HTTP request verbs.
So, in the above example you might think that we have represented end points for the following
URLs:
1 GET /create
2 POST /create
You might also be wondering how we route to our RESTful controller. You see, using the verb
methods on the Route class wouldn’t make much sense here. Well, say goodbye to routing to
individual actions. Let’s take a look at another routing method.
Controllers 93
1 <?php
2
3 // app/routes.php
4
5 Route::controller(‘article’, ‘Blog\Controller\Article’);
This single method will route all of the actions represented by our RESTful controller. Let’s take a
closer look at the method signature.
The first parameter is the base URL. Normally RESTful routing is used to represent an object, so in
most circumstances the base URL will be the name of that object. You can think of it as some what
of a prefix to the actions that we have created within our RESTful controller.
The second parameter you will already be familiar with. It’s the controller that we intend to route
to. Once again, Laravel will happily accept a namespaced controller as a routing target so feel free
to organise your Controllers however suits your needs.
As you can see, using this method to route to your controllers provides a distinct advantage over
the original routing method. With this method, you will only have to provide a single routing entry
for the Controller, rather than routing to every action independently.
And here MatureController controller is being extended from base controller but we can create our new controller as well.
You can name your controller whatever you like. Keep one thing in mind when you name a controller ,start with a capital letter
Composer would handle all the rest.

 

public function showIndex()
{
return View::make(‘index’);
}

 

Route::get(‘index’, function()
{
return View::make(‘index’);
});

Controllers are neat and tidy, and provide a clean way of grouping common logic together. However,

they are useless unless our users can actually reach that logic. Fortunately, the method of linking a
URI to a Controller is similar to the routing method we used for Closures. Let’s take a closer look.
<?php

// app/routes.php

Route::get(‘index’, ‘MatureController@showIndex’);
In order to link a URI to a Controller we must define a new route within the /app/routes.php file.
We are using the same Route::get() method that we used when routing Closures. However, the
second parameter is completely different. This time we have a string.
The string consists of two sections that are separated by an at sign (@). Let’s have another look at
the Controller that we created in the last section.
<?php

// app/controllers/MatureController.php

class MatureController extends BaseController
{
public function showIndex()
{
return View::make(‘index’);
}

public function showSingle($matureid)
{
return View::make(‘single’);
}
}

So we can see from the example that the class name is ArticleController, and the action we wish
to route to is called showIndex. Let’s put them together, with an at (@) in the middle.
1 MatureController@showIndex
It really is as simple as that. Now we can use any of the methods that we discovered in the basic
routing chapter, and point them to controllers. For example, here’s a controller action that would
respond to a POST HTTP request verb.

<?php

// app/routes.php

Route::post(‘article/new’, ‘ArticleController@newArticle’);
Just make sure to include the namespace within your route declaration and everything will be just
dandy!

<?php

// app/controllers/Mature.php

namespace MatureClass\Controller;

class Mature
{
public function showIndex()
{
return View::make(‘index’);
}
}
Let’s see how this namespaced controller can be routed to. You’ve probably already guessed it!
<?php

// app/routes.php

Route::post(‘index’, ‘Mature\Controller\Mature@showIndex’);

for more go to laravel

Please rate this