Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Laravel Routes and Controllers

Laravel Routes and Controllers

Learn Laravel Routes and Controllers for beginners

Avatar for Abanum Chuks

Abanum Chuks

November 23, 2019

Other Decks in Programming

Transcript

  1. Hello! Web Artisans I’m Chux Abanum. I build awesome Web

    Applications with Laravel and VueJS I’m an aspiring Machine Learning Engineer. Tweet Me @OilmoneDev
  2. Routes – the gateway to your application Routing refers to

    how an application's endpoints (URIs) respond to client requests. – ExpressJS In a web application, routing is the process of using URLs to drive the user interface (UI). – Meteor Routing provides you tools that map URLs to controller actions. By defining routes, you can separate how your application is implemented from how its URL’s are structured. – CakePHP
  3. Laravel Routes Directory All Laravel routes are defined in the

    route files, which are located in the routes directory. Inside the routes directory, we have four route files (api.php, channels.php, console.php and web.php). We’ll focus on the web route file “web.php”.
  4. The Web Route File (routes/web.php) The routes/web.php file defines routes

    that are for your web interface. It houses the gateways to your web application interface.
  5. HTTP Request Methods HTTP defines a set of request methods

    to indicate the desired action to be performed for a given resource, a Laravel Route is usually accompanied by request method. These request methods are referred to as HTTP verbs. These methods includes Get, Post, Head, Put, Delete, Patch, Options.
  6. Available Router Methods The router allows you to register routes

    that respond to any HTTP verb. Laravel Routes provides you with methods that you can conveniently use to push a request with a particular HTTP verb.
  7. Examples are the way to go First, we’ll assume we

    have a Laravel Project setup and it can be accessed via https://larameetup.test
  8. More on Routes – Redirect Routes If you are defining

    a route that redirects to another URI, you may use the Route::redirect method.
  9. View Routes – Map a route to a view If

    your route only needs to return a view, you may use the Route::view method. The view method accepts a URI as its first argument and a view name as its second argument. In addition, you may provide an array of data to pass to the view as an optional third argument.
  10. Route Parameters – Hello $_GET The Usual way of grabbing

    data from the uri: With Laravel, you do this differently. It’s beautiful to do it the Laravel way.
  11. More on Routes v Optional Routes Parameters v Regular Expression

    Constraints v Named Routes v Generating URL to Named Routes v Route Groups v Fallback Routes
  12. Moving from Routes To Controllers Instead of defining all of

    your request handling logic as Closures in route files, you may wish to organize this behavior using Controller classes.
  13. What is Controller? Controllers are classes that can be reached

    through the URL and take care of handling the request. - FuelPHP
  14. What are Controllers in our Legacy Apps? Controllers in our

    legacy apps are represented by classes. In the picture shown, we have Admin, Database, Helpers and User class. Well, this depends on the structure of your web project. If you have not been using MVC paradigm then chances are that your folder structure is as shown.
  15. How to Create a Controller in Laravel There are basically

    two ways to create Laravel Controller. v The Manual Method v Using Artisan Command
  16. The “php artisan” command If you have ever created a

    folder on the command line with ”mkdir exampleFolder” then we don’t have issues here. The ”artisan” command is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.
  17. How to create a Controller Continued Open your preferred terminal

    and ”cd” into your Laravel project’s root folder. Then run the following: ”php artisan make:controller UserController” Here “UserController” is the name of our controller. Controllers should be in singular case, no spacing between words, and end with "Controller". Also, each word should be capitalised (i.e. BlogController, not blogcontroller).
  18. Single Action Controllers If you would like to define a

    controller that only handles a single action, you may place a single __invoke method on the controller:
  19. Resource Controllers Laravel resource routing assigns the typical "CRUD" routes

    to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for ”posts" stored by your application.
  20. How to Create a Resource Controller Using the make:controller Artisan

    command, we can quickly create such a controller: We’ll use the command below to create a Post resource controller
  21. Defining Route for the Post Controller This single route declaration

    creates multiple routes to handle a variety of actions on the resource. The generated controller will already have methods stubbed for each of these actions, including notes informing you of the HTTP verbs and URIs they handle. Verbs URI Action Route Name GET /posts index posts.index GET /posts/create create posts.create POST /posts store posts.store GET /posts/{post} show posts.show GET /posts/{post}/edit edit posts.edit PUT/PATCH /posts/{post} update posts.update DELETE /posts/{post} destroy posts.destroy