Routing via Laravel API - laravel

I have a very small software and I use laravel and vue.js. I would like to know the difference between routing though api.php and web.php in routes folder. Can somebody explain me the difference in these two cases?

You can define the routes in either web.php or api.php. However, routes in api.php have certain advantages
routes get automatically prefixed with '/api/'
routes use api-middleware and auth. This can add certain additional security by throttling API-requests.
You should use api.php for all your api needs or any routes that will be called through ajax.
:

Related

Laravel routing pattern similar to Yii

I am new to Laravel and by reading a bit its documentation, it seems that for each new page or url I need to define a route in the web.php file. Please correct me if I am wrong.
So, my question is... Is there a way to create a pattern as in YII framework or .NET framework to manage all routes (or most of them) at once by using something like:
{controller}/{action}/{id}
Well not exactly like Yii or .net but you can declare multiple routes in one line using Resource Controllers.
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "photos" stored by your application.
You can do so by Route::resource('photos', 'PhotoController');
Read more here

Vue.js SPA with Laravel API

I have a problem with defining routes for frontend portion of the application. It is Vue.js with Vue Router. Since it is SPA I think defining all routes in Laravel is not the correct answer but it seems I need to somehow because content is not served with current setup. Does Vue Router fetch the entire page and load it with each link? That would mean setting up a route for each link to load in Laravel routes. I think this is not intention of SPA decide.
So with route like
<route-link :to="{name: 'faq'}">FAQ</route-link>
Does this mean I must make a route
Route::get('faq', 'FaqController#index')->name('faq');
This is new architecture that is not well understand with me now. Thank you.
Generally, SPA setup will fallback to "index.html" when no matched route
In routes/web.php:
Route::get('{path}', function () {
return view('index');
})->where('path', '(.*)');
and vue-router will handle the rest
full setup: https://github.com/cretueusebiu/laravel-vue-spa/blob/master/routes/web.php

Building on Laravel API backend and Vuejs frontend

I would require a little help here if this method will work out well.
Firstly, I have a backend API server created using Laravel and Laravel Passport. Secondly, I have also created the frontend with Vuejs within the same project. As such, I will be required to use both the api.php and web.php routes. I am also redirecting these routes using vue-router.
Backend
Inside the web.php routes, I have used two different routes because I want to display generic contents on my landing site and the other as an authentication required dashboard.
Example:
web.php
As above, this is to capture the routes which are 404 Not Found that are directly manipulated in the address bar to redirect correctly to their respective pages. I also ended up having two different blade templates named as dashboard.blade.php and home.blade.php respectively. Is this an okay practice for a Laravel-Vuejs project? Or are there ways that you would like to recommend?
dashboard.blade.php
home.blade.php
Login related problem with login page using the layout of the landing page into another layout of the dashboard page
The problem that I am faced with when doing an API login with the password grant is that the login page does not redirect to the dashboard page properly. The URL route does change but the page is rendered as blank.
The login using axios here:
I have managed to fix the problem.
In web.php, since we have
Route::get('/dashboard/{any?}', function () {
return view('dashboard');
})->where('any', '^(?!.*api).*$[\/\w\.-]*');
Inside of my Login.vue redirect handler, I use location.href = '/dashboard' instead of this.$router.push('dashboard').

where is the route.php file in laravel v5.3.10?

I download latest laravel project v5.3.10 but i cannot find route file and some others.
routes.php is no longer exists in Laravel 5.3 but there is a new routes directory on your root project folder, it contains two files web.php and api.php .
Those two files provide more explicit guidance in how to split the routes for your web interface and your API. The routes in the api route file are automatically assigned the api prefix by the RouteServiceProvider.
for further informations check Laravel 5.3 release notes.
You have the routes directory in the root of your application.
The route file in a directory. Please check this,
app/routes/web.php
you can write your all route files here
The web.php file contains routes that the RouteServiceProvider places in the web middleware group, which provides session state, CSRF protection, and cookie encryption. If your application does not offer a stateless, RESTful API, all of your routes will most likely be defined in the web.php file.
Read more about laravel 5.3 structure here
In the new laravel 5.3 there is no file named as routes.php in App/HTTP/
In the new laravel you can find a new folder as routes
Inside it there is web.php It is the new routes.php file
You can edit web.php file just as you edit laravel 4 or older

Missing routes.php File in New Laravel Project

I downloaded Composer, installed Laravel, and started my first Laravel project to begin learning Laravel using the lessons on laracast (great lessons). Lesson two covers routes. My new project does not have a routes.php file.
I deleted composer and started again. Same thing. Tried two different computers. Same thing. I was using NetBeans so I tried using PHP Storm. Same thing. I tried making my own routes.php file but it doesn't seem to work right because I know nothing about Laravel at this point. I tried creating and saving the project in htdocs, and then PHPStorm project folder, again - no routes.php file.
Composer is saved here- C:\Users\myName\AppData\Roaming\Composer\vendor\bin. I used composer global require "laravel/installer" in command prompt to install laravel. Any ideas?
The lastest version of Laravel doesn't have a routes.php file.
This 'routes.php' file was located in \app\Http in the older versions.
In the newer version, Laravel 5.3, we have a folder named 'routes', where we can find the following files:
api.php
console.php
web.php
For this new version, the routes for your controllers, you can put inside web.php file
See the documentation about routing here
https://laravel.com/docs/5.3/routing#basic-routing
The video lesson you are watching may be outdated.
In the Latest Laravel they have removed common routes.php where as they have added different route files to better manage your application routes.
There is
routes/web.php : routes file which works similar to routes.php file where you can have your routes and all the POST routes in web.php file will be validated for the CSRF Token similar to normal Laravel Post route.
routes/api.php : routes file where you can have your Application's API routes, the URL will be example.com/api/ Eg. If you have route getUsers then the API URL will be example.com/api/getUsers. The most important thing to notice is POST requests to an API url will not be validated for CSRF Token.
routes/console.php : routes file where you can define your Artisan commands which you can run from Laravel Artisan CLI.
Laravel new version don't have routes.php
It has
1.web.php
To create Web routes
2.api.php
if you are using front (js) framework then write routes here
3.console.php
the console.php used for console commands and interaction with commands
#Geraldo has answered it well but still something more you can learn-
In Laravel newer version, old types of routes.php file has deleted.
Why removed:
From Laravel announcement, it has done to give more flexibility to the routes.
Solution:
Now there, a route folder has added and inside that folder there are 4 files.
web.php -- The previous routes were in this files mainly. Here is where you can register web routes for your application.
api.php -- Here is where you can register API routes for your application.
channels.php -- Here you may register all of the event broadcasting channels that your application supports.
console.php -- For all of the console commands and interaction with commands.
See, now it is more flexible for you to add any API and then link it via it's api.php route file and normal route in web.php file. Thanks.
In version 5.6 there is no routes.php file under Http/Requests, from the docs:
All Laravel routes are defined in your route files, which are located
in the routes directory. These files are automatically loaded by the
framework. The routes/web.php file defines routes that are for your
web interface. These routes are assigned the web middleware group,
which provides features like session state and CSRF protection. The
routes in routes/api.php are stateless and are assigned the api
middleware group.
For most applications, you will begin by defining routes in your
routes/web.php file. The routes defined in routes/web.php may be
accessed by entering the defined route's URL in your browser. For
example, you may access the following route by navigating to
http://your-app.test/user in your browser:
Route::get('/user', 'UserController#index');
Listen
Go to
Project Folder Name --> app --> Http --> routes.php
You will find routes there.
In the newer version,
Laravel 5.3, find folder named 'routes', where following files are present:
api.php
console.php
web.php
For this new version, the routes for your controllers, you can write in web.php file
Laravel new version doesn't have routes.php
1.web.php To create Web routes
2.api.php if you are using front (js) framework then write routes here
3.console.php the console.php used for console commands and interaction with commands

Resources