# Laravel Routing Basics

Laravel's routing is responsible for passing processing to controllers that correspond to the path and content of an HTTP request.

## Directory structure
| File         | function               |
| ------------ | ------------------------- |
| api.php      | API routing               |
| channels.php | Broadcast channel routing |
| console.php  | console routing           |
| web.php      | General web page routing  |

We will generally use `web.php` for web routing. If you want to use api routing then you can do it under `api.php`.  

## Returing to view

routes/web.php

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

```

Simply we can pass the first argument as path and the second argument as callback/ closure function. Generally, the second argument is a reference to the controller class because writing business logic inside the callback function might bloat the routing file. 

If we don't have any business logic and want to redirect to view simply, we don't need to register the controller. 
`view('welcome')` will make reference to `resources/views/welcome.blade.php`

### Callallback function for PHP7.4 or later

routes/web.php

```
Route::get('/', fn () => view('welcome'));
```

### A simple way to write a view

If you just want to redirect to view, you can use the `view` function of Route.

routes/web.php

```
Route::view('/', 'welcome');
```
If you want to show some texts only you can do it as well. 

routes/web.php

```
Route::get('health-check', fn () => 'ok');

```

## Routing Priority

We should always declare static routes first because any dynamic would be executed first if they're declared before static routes.

routes/web.php

```
Route::get('articles/{id}', [ArticleController::class, 'show']);
Route::get('articles/create', [ArticleController::class, 'create']);
```

In the above example, when accessing '/articles/create', the route parameter  `{id}`  is prioritized instead of  `create`  and the show method will be invoked.  Correct implementation would be as follows

routes/web.php
```
Route::get('articles/create', [ArticleController::class, 'create']);
Route::get('articles/{id}', [ArticleController::class, 'show']);

```

## Redirect

If we want to do the simple redirect, the `redirect` function can be used. The original route will return  `302` as  a status code.

routes/web.php
```
Route::redirect('here', 'there');

```
In this example `/here redirects to `/there`.

## HTTP request method

Routes can be registered corresponding to HTTP request methods.

routes/web.php

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

```

## Resource Routes

routes/web.php
```
Route::resource('articles', ArticleController::class);

```
If we use the `resource` function in the route, it will be the same as writing the following way.

routes/web.php

```
Route::get('articles', [ArticleController::class, 'index'])->name('articles.index');
Route::get('articles/create', [ArticleController::class, 'create'])->name('articles.create');
Route::post('articles', [ArticleController::class, 'store'])->name('articles.store');
Route::get('articles/{article}', [ArticleController::class, 'show'])->name('articles.show');
Route::get('articles/{article}/edit', [ArticleController::class, 'edit'])->name('articles.edit');
Route::put('articles/{article}', [ArticleController::class, 'update'])->name('articles.update');
Route::delete('articles/{article}', [ArticleController::class, 'destroy'])->name('articles.destroy');

```


## Official Document
For further information please look into the official Laravel documentation 
https://laravel.com/docs/9.x/routing
