I have https://tenancyforlaravel.com/ installed in laravel to make multi-tenant and it works fine for the web routes.
My problem is that when I access my APIs then I get a 404 error in tenant domains.
tenancyforlaravel documentation: https://tenancyforlaravel.com/docs/v3/routes
It says that I must put all my APIs inside api.php file and wrap them in a Route group with this middleware so I put all my APIs inside api.php file and all my APIs as below:
Route::middleware('tenancy')->group(function () {
Route::name('api.')->namespace('Api')->group(function () {
Route::post('/login', 'AuthController#login')->name('login');
...
});
and when I access it using sub.local.test/api/login then I get 404 error.
Tested for tenancyforlaravel.com V3 and it works OK.
Route::middleware([
InitializeTenancyByDomain::class,
PreventAccessFromCentralDomains::class
])->prefix('api')->group(function () {
//
Route::name('api.')->namespace('App\Http\Controllers\Api')->group(function () {
Route::post('/login', 'AuthController#login')->name('login');
...
});
Put all your API routes inside api.php as below
use App\Http\Controllers\AuthController;
Route::group(['prefix' => '/{tenant}',
'middleware' => [InitializeTenancyByPath::class],],
function () {
Route::post('/login', [AuthController::class, 'login'])->name('login');
...
});
As you haven't mentioned your tenant identifier, I am using path as identifier, so using InitializeTenancyByPath middleware. Use whatever identifier middleware you want in place of that.
Access your API routes normally as you used to do, with your identifier. As this example uses path as identifier, the endpoint will look like:
sub.local.test/api/{tenant}/login
Related
location #laravelapi {
rewrite /api/(.*)?$ /api/index.php?$is_args$args last;
}
When we try access to api/routeexample, laravel gets only routeexample part, and throws 404 error. How we can send full url including 'api' part?
I m adding an example of how we define route for API request:
In routes/api.php
Route::group(['prefix' => 'v1'], function () {
Route::post('api_request', [App\Http\Controllers\YourController::class, 'targeted_function_name'])->name('api_request');
});
So I'm just added
include "api.php";
into my routes/web.php as hotfix, but i will search for better solution
I have the following route declaration:
Route::name('test.')
->prefix('test')
->group(function() {
Route::get('/', function() {
dd('test');
})->name('index');
});
Trying to access /test will result in NotFoundException, although when calling route('test.index') it will resolve to /test.
Same exception when trying to access /test/ and when trying to change the line:
Route::get('/', function()...
to
Route::get('', function()...
As soon as I modify it to
Route::get('/test', function()...
it works when trying to access ´/test/test´ and it also resolved the uri correctly when calling route('test.index').
What am I missing to to get the route working using '/' ?
need some help. I'm trying to fetch data from my db using axios. My backend is Laravel. I have a 200 status http request but it returns the whole html not the data I'm expecting.
Here is my code for route
Route::get('/home', 'PostController#ajaxCall');
Route::post('/home', 'PostController#store');
Route::get('/{any?}', function () {
return view('welcome');
});
Here is my code for Home.vue for Axios request
export default {
components: {addForm},
data () {
return{
posts:[]
}
},
created() {
axios.get('/home').then(response => this.posts = response.data);
}
}
For my controller
public function ajaxCall(){
return response(Post::all());
}
It looks like you get to the ajaxCall() method by using the route '/home', but with axios, you are hitting "/" which returns a view called Welcome. Maybe you need to change the path you use in axios to '/home'?
It might be late but maybe someone else is looking for the solution
I was also facing this in SPA using laravel and VUE.
Route::get('/{any}', 'SinglePageController#index')->where('any', '.*');
SPA has this route, so whenever you write any other get route your app will redirect you to the home page, to avoid this either move this route at the end of your web.php
or write your other routes in api.php file.
In my case i solved it by changing the GET method to POST method.
Try that. It might help
I'm new to Laravel and PHP in general, but familiar with Vue and SPA's. I know how to create authentication with Bcrypt, for example. However, I used Laravel to run php artisan make:auth and to create several different endpoints for the backend.
I was trying to convert my app into a SPA using Vue, however using routing with Vue causes issues with the routes defined in the web.php. For example, I have my web.php like this, with several different routes.
<?php
Route::get('/{vue_capture?}', function () {
return view('app');
})->where('vue_capture', '^(?!storage).*$');
Route::get('/', function () {
return view('app');
});
Route::resource('Videos', 'VideoController')->middleware('auth','isAdmin');
Route::resource('Categories', 'CategoriesController')->middleware('auth');
Route::get('/search', 'VideoController#search')->middleware('auth');
Auth::routes();
Route::get('/settings/account', 'AccountsController#edit')->middleware('auth');
Route::get('/auth', 'AccountsController#get');
Route::put('/settings/account', 'AccountsController#update')->middleware('auth');
However, I also have a routes.js to have vue-router handle the routing with the following :
import Home from './components/Home.vue'
import Videos from './components/Videos.vue'
import Categories from './components/Categories.vue'
export default{
mode: 'history',
routes: [
{
path:'/',
component:Home
},
{
path:'/Videos',
component:Videos
},
{
path:'/Categories',
component:Categories
},
{
path:'/login',
component:login
},
{
path:'/register',
component:register
},
{
path:'/logout',
component:logout
}
]
}
I understand that Vue is supposed to take over the routing if you use a SPA, but is there any way to use both? I can't find anything that addresses this issue but I can't believe that Laravel would make you choose to either use their built-in commands like php artisan make:auth or have to scrap all of that and do it all manually if you want a SPA with routing.
I've tried going through different CRUD turorials on using Laravel and or Vue.
I've tried directing all of the routes to Vue to have vue-router handle the routing.
I've tried not giving all routing to vue-router and retaining the back-end routes that I had in web.php
Other semi-related problem is my routes aren't even appearning as links using router-link . It just appears as normal text. But for now my priority is the routing issue.
You can combine both vue route and Laravel route. But for the best result, I advise you use vue router since you are building an spa.
remove
Route::get('/', function () {
return view('app');
});
put all the backend route before the route that point to your vue.
Route::resource('Videos', 'VideoController')->middleware('auth','isAdmin');
Route::resource('Categories', 'CategoriesController')->middleware('auth');
Route::get('/search', 'VideoController#search')->middleware('auth');
Auth::routes();
Route::get('/settings/account', 'AccountsController#edit')->middleware('auth');
Route::get('/auth', 'AccountsController#get');
Route::put('/settings/account', 'AccountsController#update')->middleware('auth');
Route::get('/{vue_capture?}', function () {
return view('app');
})->where('vue_capture', '^(?!storage).*$');
Also, verify that you don't have conflicting routes on your backend (run php artisan route:list to see your laravel route list) and vue routes. I hope this helps.
The accepted answer is perfect (I upvoted but I have no rep yet for it to count) and worked well for me but only under the condition if I also created 'dummy' vue components and imported them to work with the routes. Hope this helps!
import Login from './auth/Login'
import Register from './auth/Register'
import Logout from './auth/Logout'
{ path:'/login', component:Login },
{ path:'/register', component:Register },
{ path:'/logout', component:Logout },
You can just use this instead
Route::get('/{vue_capture?}', function () {
return view('app');
})->where('vue_capture', '^(?!storage).*$')->middleware('auth');
I have a situation. I am trying to create an application that will have only one route file, api.php for both the web app (spa) and mobile app.The problem is now that the entire application is stateless (as it should be), I can't even login. Because, routes in api.php expect a token in the request header, which I don't know how to provide.I am using vue in the front-end I have this simple strategy:<button #click="login">Login</button>and the login method looks like this:
login(){
axios.post('login',this.credentials)
.then( window.location = "http://localhost:3000/app" );
}
And my route definition:
Route::group(['middleware' => ['role:admin']], function () {
Route::get('app', function () {
return view('index');
});
});
But it redirects me back to the login page. I wish I could do something like window.header = Bearer myLongToken. I am using JWT, if that helps.
Update:
It looks like there is something else going on. If I remove the role:admin middleware, then I get redirected to desired route, but if I add the middleware, I get redirected back to the login route even if the credentials are valid.
// Route::group(['middleware' => ['role:admin']], function () {
Route::get('app', function () {
return view('index');
});
// });