I'm completely new to Laravel and have only coded with PHP manually using long and repeating functions. I basically know nothing about coding with frameworks and all these manuals and instructions are nonsense for me.
Could you please, explain what routing, controllers and maybe other things in Laravel are? Like for a noob using an example.
I had no problem coding without a framework, but now it's like learning to code from the base. I don't know how can a framework help to simplify my work in that way.
I'm tired of writing a huge and repeating code, though.
Manuals in Laravel documentations or other sites are complicated and intended for someone who already has the experience.
Thank you!
If you are new to Laravel a good place to start is the following series on Laracasts https://laracasts.com/series/laravel-5-from-scratch.
Basically to answer your question, routing is the process of taking an http request and converting that specific request into a pathway (route) that needs to be followed to an endpoint where the endpoint is the code that handles the specific request.
I presume you know the difference between http verbs, like GET, POST, PUT and DELETE. Laravel routes allow you to define and listen for a specific verb and then map that verb+request to a specific piece of code, either inside a closure or the endpoint could be a reference to a controller.
Controllers basically are code containers that services and handles the http layer of your application.
So in simple terms the router delegates the incoming request to a controller to handle the request and return a response.
Lets look at an example:
So basically we have a GET http verb requesting the blog path (or route). To service this request in Laravel you would then have.
Route::get('/blog', function () {
echo "Handle the specific request";
});
Now the above code will catch the /blog request and the closure will service the response. To hand that same request off to a controller you specify the controller class and the method.
Route::get('/blog', 'BlogController@index');
So above we have a BlogController
class that will contain and index
function that will handle and return the response for the /blog request.
Class BlogController extends Controller
{
public function index()
{
return 'Response for the blog request.';
}
}