I have an application with a Laravel back-end/React.js front-end. I am using react router so to make that work I have any path pointing to the Controller which loads the view with the components in them. However when I add nested routes in the react side i.e. "blog/:blogId", it seems that it completely skips the tag in which components are loaded into and shows me content from the Laravel view instead.
I originally had:
`Route::get('/{slug?}', 'ProjectsController#index'`
I've tried:
`Route::any('{path}', function($path){
$projects = Project::all();
$posts = Post::all();
return view('welcome', compact('projects', 'posts'));
})->where('path', '.*');`
This won't work either:
`Route::get('/{slug?}/{id?}', 'ProjectsController#index'`
What I want is for both /blog and /blog/2 to go to ProjectsController#index which loads the view with the react tag.
The data in my index function in ProjectsController is:
`$projects = Project::all();
$posts = Post::all();
//return $projects;
return view('welcome', compact('projects', 'posts'));`
Has anyone come across this before? Thanks.
UPDATED:
The welcome view being returned by the controller index method:
The <div id="content"></div> is where my base component is being loaded into. So when navigating to route '/blog' I see my component, when navigating to '/blog/anything' I see Laravel's login and register links.
`#extends('layout')
#section('content')
#if (Route::has('login'))
<div class="top-right links">
#if (Auth::check())
Home
#else
Login
Register
#endif
</div>
#endif
<div id="content"></div>
#include('partials._scripts')
#endsection`
The component making use of the link:
`View here
<Route path="/blog/:blogId" component={Post} />`
My other routes in the base component:
`<Switch>
<Route exact path="/" render={(props)=> <Home text={text} mobileNavIsOpen={hamburgerOpen} backgroundImage={background}/>} />
<Route path="/projects" component={Projects} />
<Route path="/blog" component={Blog} />
<Route component={NotFound} />
</Switch>`
I've tried placing the /blog/:blogId route in the base app along with the others - still no difference.
UPDATE:
When removing the /blog/ route tag from react router and just keeping '/blog/:blogId, it has the same effect.
UPDATE/PARTLY SOLVED:
Adding (\\d+) to my react router path: path={/blog/:id(\\d+)} rightly took me to the nested component. It did also load the blade view content, however, so as a temporary fix I have taken out the blade content that was displayed. Would be good to see if anyone has another way around it. Thanks.
Try this
Route::get('/{any}', 'ProjectController#index')->where('any', '.*');
This is basically added to the end of the routes/web.php, since most probably you are trying to build a SPA and you want all http requests to be handled by react router.
Update:
Based on your updates I understand that you want both /blog and /blog/:ID to be handled by the same method. That can be done like this
Route::get('blog/{id?}', 'ProjectController#index')->where('id', '[0-9]+');
If you need the ID in the index method give it as the first argument to the method and send it to the view.
Related
I have a weird situation, where my route seems to be not defined.
I am using Laravel's starter kit Breeze, and I am adding top left navigational links. This is my 4th link already, but it seems that It's the first time I face such issue.
Controller:
public function index(Request $request)
{
$user = $request->user();
$data = $user->campaigns;
return view('subscribes.index', ['data' => $data]);
}
Route:
Route::resource('/my_campaigns', App\Http\Controllers\SubscribeController::class);
Navigational link code:
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<x-nav-link :href="route('subscribes.index')" :active="request()->routeIs('subscribes.index')">
{{ __('My Campaigns') }}
</x-nav-link>
</div>
Error message:
Route [subscribes.index] not defined.
I have spent 2 hours trying to check every single spot, where the issue may lay, but to no avail. I have already cleared route:cache.
I'd like to note, that the page /my_campaigns opens without any issues, and is working, until I try to add it within the navigational links.
My folders and files seems to be created correct as well, due to the fact, that /my_campaigns is working.
Any idea, where I missed something?
The issue was within my navigational link code. Where the href route is defined, I have used incorrect URL, therefore, this issue was caused.
In laravel I have a vue.js main view template, what i want is when i click on main template this displays the child model
//my main template
<template>
<div>
<ul>
<li #click="chatWith()"></li>
</ul>
</div>
</template>
<script>
export default {
methods: {
chatWith(){
return {
template:<conversation-with></conversation-with>
}
}
},
}
</script>
but is not working, another code is working on click, but goes to template isn't
If you want it to route to a new page you need to create a route that points to that template somehow. You can either set up that template on a blade and use laravel's routing in web.php to create a route for it, then link to it like a normal <a href="/chatWith">, or you can similarly create a route using vue router instead, which would allow you to have single-page apps with better routing control.
https://laravel.com/docs/5.8/routing (make sure it's your correct laravel version)
https://router.vuejs.org/guide/
if you need it to be some kind of modal or something that's another story though
I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.
Hi I have a hyperlink from a page:
<h3>hitest</h3>
the route:
Route::get('hitest', function(){ return 'hitest message';});
There is an error:
No query results for model [App\User2].
hyper link is from a page with this url
/userpage/1
the 1 is a model object.
Shouldn't the hyperlink route to /hitest ?
Please see my other post: Strange behavior with routing and hypertext.
I'm new at web development. Is there configurations for routing? The app is hosted (not local).
As bytesarelife already mentioned, you can use the url()-function, like so:
{{ url('your/url/') }}
A better way in terms of maintainability would be to give your routes names, so you would not have to replace every url in every template once you want to change it. You can do so, by adding the name in your routes:
Route::get('hitest', function(){ return 'hitest message';})->name('getHittest');
And then you can use the route function in your view:
{{ route('getHittest') }}
I am setting up my own admin for a Laravel project and everything is going along fine until I hit what seems to be a routing issue. Here's my situation so far.
Within my views folder I have a folder named panel which holds all of my views for the admin panel. This is working perfectly. I have full access to the panel without a problem. Within the panel directory I have a folder named users which holds my views for the UsersControllers. This is where I'm struggling. My route for those views is as follows:
Route::resource('users', 'UsersController');
Route:list shows those routes as users.index, users.store etc.
In the top nav bar of the panel I have a link to the users index as
<li>Users</li>
I've also tried using
<li>Users</li>
Either way, this should be calling the index() method of the UsersController. That method looks like this
public function index()
{
return view('panel.users.index');
}
I've also tried just
public function index()
{
return view('users.index');
}
No matter what I try I get
NotFoundHttpException in RouteCollection.php line 161:
I would really appreciate a bit of wisdom on how to resolve this one.
you can use this for index
<li>Users</li>
or you can use action
<li>Users</li>