Routing

Routes are defined in routes/web.php file. As mentioned earlier, routes are basically an array with key names for the URL, and the name of the Controller class and method to be called for the URL as value.

Default Routes

Before defining routes, you may need to declare the default route for landing and error pages. 

To do that, open config/url.php file and modify the value of landing key for the default landing page, and error key for the default error page of the project. 

As value, you must type the name of the controller class first, then method of the class to be called after @ sign-

HomeController@welcome

Above, ‘HomeController’ is the name of the controller class and ‘welcome’ is the name of the method. 

You can define the URL for accessing database migration page as the value of migration_url key. The URL must exclude the domain name and first forward slash. Once you go into your browser and access the URL (by adding a forward slash and the value of the aforementioned key after your domain name) you will see the database migration page.

You can also define the prefix for the URLs that will receive API calls in the api_url key. The framework will automatically include the prefix at the beginning of the API routes that you have defined.

Defining Web Routes

To define web routes, open routes/web.php file. There, you can define the URL for the route as a key of the array returned, and name of the controller class along with the method called from the class as its value-

‘welcome’ => 'HomeController@welcome'

In the above example, for the your-site-url/welcome URL, welcome method from the HomeController class is executed. Like default routes, you have to type the method name after the controller class name and @ sign.

Defining API Routes

To define API routes, open routes/api.php file. There, you can define the API routes similarly to how the web routes are defined above-

'test_api' => 'HomeController@testApi',

It works almost exactly as the web routes. Notice that you must exclude the API prefix defined in config/url.php while defining your API routes. Call your-site-url/api-prefix/test_api url from your browser/postman to receive your API data.