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.
Related
I've come across a peculiar bug in a laravel project of mine,
In my web.php I have a named route 'welcome' :
Route::get('/', function () {
return view('welcome');
})->name('welcome');
This route contain my index page in which there's a navigation component.
Through an AppServiceProvider I'm passing data to the navigation component
The data im passing is a simple table containing this :
{
name : "home",
href : "route('welcome')"
}
<nav>
#foreach
{{ myData->name }}
#endforeach
</nav>
The problem is that when I click on the link "home" it sends me to this address :
http://127.0.0.1:8000/route('welcome')
https://i.stack.imgur.com/FaET1.png
Instead of http://127.0.0.1:8000/
and this is a problem with every single link, it always returns
http://127.0.0.1:8000/route('myLink')
this is my complete web.php for further information
Route::get('/', [HomeController::class, 'index'])->name('welcome');
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
require __DIR__.'/auth.php';
Route::resource('/nav', NavController::class);
// // Fallback page error 404
Route::fallback(FallbackController::class);
If you need more information please ask me, i've been trying to fix this issue for 2 days, and it is frustrating me.
I tried route:clear, cache:clear, view:clear and nothing changes.
Is it the format at which I've stored the name of the route that isn't working ?
The thing is it was working for an hour or two. and now impossible to fix it.
I hope someone here can help.
Josh's solution is working.
The route('welcome') was passed through as a string. and the function route() was not called.
In your data, for the key href you just need to store welcome instead of route('welcome'). Then in your blade template, you can do
login here
The reason your solution is not working is that you just have the word route stored as a string, you aren't actually calling the function called route.
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.
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 have been struggling with something very simple here nevertheless the vagueness around Laravel's routing system that complicates its easy routing approach. I have gone through questions listed here but nothing seems to help me so here it goes.
I have formerly defined a route to my controller on an action named "create". This action is suppose to accept a post data from the form and persist it. This create method has one parameter which defaults to null for a project id if its add else we pass an id e.g domain/projects/add/22 to edit and domain/projects/add to create a new one.
Below is the skeleton of the function:
public function create( $id = null ){ ... }
I then defined a route for this which is:
Route::post( 'projects/add', 'ProjectsController#create' );
Inside my form I have {{ Form::open(array('url' => 'projects/add', 'method' => 'post')) }} .
I keep on getting errors related to routing, Http or method not found exceptions. I tried to follow every suggestion on the net but cannot for the life of me find my way.
Please help me point to the right direction, thanks.
try with below sample code
routs.php►
Route::post('projects/add/{id?}',array('as'=>'project_create','uses'=>'ProjectsController#create'));
ProjectsController.php►
class ProjectsController extends BaseController{
public function create( $id = null ){
...
}
projects.blade.php► (for used blade templating your views should have blade.php extention )
<html>....
<form action="{{ route('project_create') }}"method="post">
....
</form>
</html>
Thank you guys for all your responses. After being swamped with work I finally came back to my project and wanted to try out some of your suggestions but I failed.
I did find a tutorial that I tried to follow and basically was able to have my routes working.
Inside the routes I added (see below):
Route::get('/projects/list', [
'as' => 'post.list',
'uses' => 'ProjectsController#listProjects'
]);
Inside my controller I just created a function that is named listProjects(). As for getting my form displayed I followed the same pattern except point to newProject() method in my controller.
As much as I was not keen on this approach I ended up creating another function just for saving my POSTed form data after a new project form has been filled out and submitted. I still used the same url as projects/add except pointing it to a different function in may controller named saveProject().
About the view I just added the as part of the same save route and it worked. below is a link to the tutorial I followed and taking a look at the code.
http://www.codeheaps.com/php-programming/creating-blog-using-laravel-4-part-1/
So sorry to bother y'all, but I was wondering if anyone could help me. I'm having a wee bit of a problem with my code in Laravel, being used to working from scratch rather than with frameworks. I made use of the Laravel Bootstrap Starter Site and I'm trying to add additional pages, but the routing isn't exactly co-operating. It's rather frustrating.
The Controller: app/controller/community/CommunityController.php
<?php
class CommunityController extends BaseController {
public function index() {
return View::make('community.index');
}
}
?>
The View
#extends('site.layouts.default')
{{-- Content --}}
#section('content')
#foreach ($posts as $post)
<div>
I'm just going to put this here...
</div>
#endforeach
{{ $posts->links() }}
#stop
And finally, last but not least, my routes.
Route::get('community', array(
'uses' => 'CommunityController#index',
'as' => 'community.index'
));
Now, I have this nagging feeling that I'm missing something rather small, but for the life of me I can't figure it out. If anyone would be so kind to explain what I'm doing wrong, I'd appreciate it. Especially since I can prevent this kind of problem happening in the future as well.
With friendly regards,
User who still hasn't picked out a good name
Edit: Sorry I forgot to mention this. I removed public, so I don't know if that influences anything. If it does, again, sorry for forgetting to mention this in the beginning.
you can try to bind the whole route to the controller, using
Route::controller('community', 'CommunityController');
then in your controller you have to prefix the controller methods with HTTP verbs.
Your index() method will be
public function getIndex() {
return View::make('community.index');
}
Just fire composer dump-autoload in root of your project folder from terminal / console.
It'll load your controller which is in subfolder.