Good day to all! When 'untying' from web.php as follows:
Route::get('/{page}', IndexController::class)->where('page', '.*');
the router looks like this:
import Vue from 'vue'
import VueRouter from "vue-router";
Vue.use(VueRouter)
export default new VueRouter({
mode: "history",
routes: [
{
path: '/parts', component: () => import('./components/Part/PartIndex'),
name: 'part.index'
}
]
})
The IndexController is triggered and goes to the main page. Next, the idea begins to work api.php .
Route::prefix('parts')->group(function () {
Route::get('/', PartIndexController::class);
});
However, when the axios.get('/api/parts/') method is called from the component, the IndexController is triggered again and returns the home page instead of the actions that PartIndexController should have performed.
If you remove the web in the first route.php ->where('page', '.*'); Part IndexController is triggered, but does not work correctly (outputs 404 on reboot).
I ask for help.
The fix is simple, move your default route at the end of the routes file.
i.e.
move the route
Route::get('/{page}', IndexController::class)->where('page', '.*');
at the end of your routes file.
Why?
Since you have the dynamic route /{page} all the routes below the dynamic routes are considered at the dynamic parameter to the dynamic route.
hence when you are triggering parts url, it's triggering the root dynamic route with page parameter as parts.
Note: It's not recommended to have a root dynamic route.
Hope this helps.
Best.
Here is the solution:
where('any', '^(?!api).*$');
Related
What I have is an existing laravel application with blades , laravel routes and a few vue components. So I don't want to replace the existing routes with vue router. What I want is to add additional routes without disturbing the existing laravel routes.
For an example I have a category which already is using the following category
Route::get('category/index' , 'CategoryController#index')->name('category.index');
Then I would like to add a new route using vue router without disturbing the category route
For an example:
import Dashboard from "../views/Dashboard.vue";
const routes = [
{
path: "/",
name: "Dashboard",
component: Dashboard,
meta: {
requiresAuth: true
}
}
]
Is this possible?
Update: This is what I did (If anyone can point out what am I doing wrong, it would be greatly appreciated)
1.0 Install Vue Router using npm
2.0 App.js (Use Vue Router)
import VueRouter from 'vue-router';
Vue.use(VueRouter);
const routes = [
{ path: '/dashboard', component: require('./components/Dashboard')},
];
const router = new VueRouter({
routes, // short for `routes: routes`
mode: 'history',
});
3.0 Web.php
Route::get('/{vue_capture?}', function () {
return view('testing.main');
})->where('vue_capture', '^(?!storage).*$');
4.0 main.blade.php(in testing folder)
#extends('layouts.app')
#section('content')
<div class="container" id="app">
<router-view></router-view>
</div>
#endsection
5.0 It doesn't work
Thank you.
Yes I believe this should be possible.
You will need to add a Laravel route that captures all the vue routes and shows a view which includes the vue routes.
Route::get('/vue/{vue_capture?}', function () {
return view('vue.index');
})->where('vue_capture', '[\/\w\.-]*');
You should be able to include both this route and the original Laravel ones. One option would be to prefix the view routes as shown above with /vue, alternatively if you put the routes in the correct order you should be able to avoid the prefix, if you so wish.
The php artisan route:list command will help you, by allowing you to see what the current routes are.
Yes you can and simple.
All you need is to put the existing route at the top over the route that handles for vue app.
it should look like this.
web.php
// existing route
Route::get('category/index' , 'CategoryController#index')->name('category.index');
// handle vue app
Route::get('{path}', 'VueController')->where('path', '(.*)');
VueController
public function __invoke()
{
reuturn view('index');
}
for working example you can read this section to work with vue-router vue-router
So I've got the current route setup:
Route::get('/{id}', 'MainController#index');
This is for passing in a id through / but I would also like:
Route::get('/admin', 'AdminController#index');
but it keeps handling admin as a URL parameter rather than it's own route, is there a way of distinguishing between the two?
You need to move this route to the end of the routes file to make all other routes similar to '/admin' work:
Route::get('/{id}', 'MainController#index');
I am building an app with Lumen for the backend and angular for the frontend. Lumen handles routes and serves the basic templates with a header and footer, where angular takes over to control the content. I am trying to add a url parameter to a route, but it breaks all my paths to scripts as it sees it as a subdirectory not a parameter. My route looks like this in Lumen:
$app->group(['prefix' => 'user', 'middleware' => 'auth'], function($app) {
$app->get('{any}', function() {
return view('index');
});
$app->get('detail/{userId}', function() {
return view('index');
});
});
I have a url of example.com/user/create that works fine, but as soon as I use example.com/user/detail/101 it breaks. How do I set it up so all my angular paths are not destroyed as I add parameters? I would like to stay away from adding the absolute url path as I really don't want to manage differing urls through dev/stage/production environments.
EDIT:
The following routes work and do not break css/script paths:
example.com/user
example.com/user/create
The following route does break paths:
example.com/user/detail/101
Add the any route after the detail/{userId} route.
In general, any "catch-all" routes need to go at the end so they don't interfere with anything.
I'm using the extension laravel-menu in my Laravel application.
This application contains multiple projects with multiple locations attached to each project.
Now I want to define a sidemenu where I can among other manage the locations.
The url of a project is
project/1
The url of the locations page of a project is
project/1/locations
How to setup this side menu in routes.php?
My routes.php code:
Route::resource('project', 'ProjectsController'));
Route::resource('project.locations', 'LocationsController');
Menu::make('sidemenu-project', function($menu) {
$menu->add('Locaties', array('route' => 'project.locations.index','{project?}'))->data('id',1); // this is not working
});
This is outputting the url /project/%7Bproject%7D/locations
Go to your terminal (Command Prompt) and run following command:
> php artisan routes
Then you'll see all the declared routes with their URL and corresponding route name and method name.
I'm very new to Laravel but the Routes page of documentation mentions you create a controller with parameters like this:
Route::get('user/{id}', function($id) { ... });
could you therefore define your route as
Route::get('project/{id}/locations', function($id) { ... });
I think you have this issue due to misconfiguring the routes. To achieve the route structure that you want, you should put your project/1/locations route definition above the first one. Consider your routes.php to be:
Route::resource('project/{project}/locations', ['as'=>'project.locations', 'uses'=> 'LocationsController']);
Route::resource('project', 'ProjectsController'));
I have PagesController defined in my routes file:
Route::controller('/', 'PagesController');
But i use some more routes like:
Route::get('/admin', function()
{
....some code here
});
My second route doesn't work, because all other routes try to find functions in PagesController. I can change my controller to:
Route::controller('pages', 'PagesController');
But then in my home page, all links will be like www.test.com/pages/..., but i don't need that 'pages' in there. How to define my controller with mask or something like that?
Laravel allows you to easily define a single route to handle every action in a controller using simple, REST naming conventions. First, define the route using the Route::controller method:
Route::controller('pages', 'PagesController')
This is a single route to define all actions in a controller using REST naming conventions therefore you get the /pages.
For the root of your app you need to specify the method that you want to call within your PagesController.
Example:
Route::get('/', array('as' => 'home', 'uses' => 'PagesController#getIndex'));
Place this line at the top of your routes in the routes file.
Change the order of your route definition to this:
Route::get('/admin', function()
{
....some code here
});
Route::controller('/', 'PagesController');
It will now look for /admin first, and if it cant find it, it when they go to your other routes...
Try changing Route::controller({same content as the question}) to Route::resource({same content as the question})