Laravel Basic Routing

Basic Routing

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

Route::get('/', function () {
 return view('welcome')
});

E.g.:

Route::get('/', function () {
 return "Hello Laravel";
});

Output:

Laravel Basic Controller

We don`t have to create any files here, to do anything in the URL. We can`t even pass IDs and parameters through this.

E.g.2:

Route::get('/admin/posts', function () {
 return "Hello Admin Is Here";
});

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:

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback); 

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:

Route::match([‘get’, ‘post’], ‘/’, function () {
//
});
Route::any(‘/’, function () 
{
//
}); 

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.

@csrf ------------
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:

Route::redirect(‘/abc’, ‘/xyz’);

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

Route::redirect(‘/abc’,  ‘/xyz’, 301);

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

Route::permanentRedirect(‘/abc’, ‘/xyz’);

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:

Route::view(‘/welcome’, ‘welcome’);
Route::view(‘/welcome’, ‘welcome’, [‘name’ => ‘John’]);