UNDERSTANDING LARAVEL ROUTES

Today we will see how we can use routes file(app/routes.php) in Laravel to define our application routes . If you are beginner i strongly recommend you to go through Getting Stated with Laravel tutorials . For Instance if your application need to respond to GET URI and send back JSON response in laravel below line will do this .

In the above code we see get method is taking two parameters first is the URI to which application should respond and second is closure callback which will be executed if the URI matches . Some of the other methods available in route class are

Now If you navigate to /cakephp then CakePhp will be printed for /codeigniter CodeIgniter will be printed and for /laravel it will print Laravel is Beautiful . Thats so easy . Next lets see how to post data from HTML Form . First create a HTML Form using open method of Form class and pass our url to which data will be posted and set method as POST . We have also put two input fields so we can access it in our route

Now go to /testing and type something in username , password fields and press Login you will see var_dump of Input data It will contain username,password and an extra field known as _token which will safeguard our request from CSRF

We can also match multiple verbs for single route using match method of Route class

Author

Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

About Us

We develop custom web apps with architecture that is carefully engineered
and tested for optimum use of Memcache to balance the read/write activity for the lowest
possible CPU usage on the server. We use the best programming languages and Frameworks
for the app, such as Laravel (PHP), Angular JS (Javascript) or RoR (Ruby).