Laravel Basic Routing

Basic Routing

The basic Laravel routes accept a URI and a Closure, providing a straightforward method for defining routes:

E.g.:

Output:

Laravel Basic Controller

We dont have to create any files here, to do anything in the URL. We cant even pass IDs and parameters through this.

E.g.2:

Output:

Laravel Basic Routing

We can write whatever we want, and we can do this too.

‘/admin/posts’  once we write this,

    return “Hello Admin Is Here”;  we can write this quickly.

We can make ( ‘/admin/posts’  ) this as long as we want too.

Route Files:

All routes are defined in route files that are located in the routes directory. The routes/web.php file defines routes for our web interface. The web framework automatically locates these files.

These routes are used to set the web middleware group that provide features like session state and CSRF protection. The routes in routes/api.php set the api middleware group.

For many applications, we will begin by defining routes in our routes/web.php file. The routes are defined in routes/web.php that might be accessed by entering the defined routes URL in our web browsers.

Routes are specified in the routes/api.php file. These are nested within a route group by the RouteServiceProvider

We can modify the prefix and other route group options by changing our RouteServiceProvider class.

Available Router Methods:

The router allows us to register routes which respond to any HTTP verb:

Sometimes, we need to register a route which responds to multiple HTTP verbs by using match method or we may even register a route that responds to all HTTP verbs using the any method:

CSRFProtection:

Any HTML forms are pointing POST, PUT or DELETE routes that are defined in the web routes, the file should include a CSRF token. Otherwise, the request will be rejected.

Basic Routing

RedirectRoutes:

If we are defining a route which redirects to another URI, we may use the Route::redirect method. This method provides a convenient shortcut by which we do not need to define a full route or controller for performing a simple redirect:

By default, Route::redirect returns 302 status code. We can customize the status code by using the optional third parameter:

We can use the Route::permanentRedirect method that returns a 301 status code.

View Routes:

The view method accepts a URI as a first argument and a viewname as its second argument.

We provide an array of data to pass the view as an optional third argument: