Laravel: Named route not found - laravel

I have the following named route in routes.php:
Route::get('/', 'DefaultController#index', array('as' => 'home'));
Then, I try to get a link to the route in another view (404.blade.php), based on docs:
Go to homepage
However, the page with the code above thows this: Error in exception handler: Route [home] not defined. I tried using simply route('home') as well.
What am I missing?

The syntax for named routes is a bit different
Route::get('/', array('as' => 'home', 'uses' => 'DefaultController#index'));

Related

Laravel route gets wrong controller

I'm building project with Laravel and Vue and i want my categories and tags urls to be like that:
domain.com/some-tag
domain.com/some-category
My web.php:
Route::get('/', ['uses' => '\App\Http\Controllers\IndexController#index']);
Route::get('/{category}', ['as' => 'category', 'uses' => '\App\Http\Controllers\CategoryController#index']);
Route::get('/{tag}', ['as' => 'tag', 'uses' => '\App\Http\Controllers\TagController#index']);
Route::get('/{category}/{article}', ['as' => 'category.article', 'uses' => '\App\Http\Controllers\ArticleController#index']);
I'm getting 404 error on my tags links and i know its because router matches "category" first and uses CategoryController.
What should I do? I don't want to make them unique by adding something like domain.com/tags/tag-name
I've tried to use named routes for my vue component (with Ziggy-js lib) so my link looks like
<a class="tags-block__link" :href="route('tag', {tag: tag.slug}).url()" v-for="tag in tags" :key="tag.id">
But it doesn't help
Why it should not be mixing?
You define Route::get('/{category}' and Route::get('/{tag}'. So if you open /1 in your browser it will always run the first route it is able to find that matches the pattern. So it is always running CategoryController#index yes?
Your routes should be:
Route::get('/category/{category}', ['as' => 'category', 'uses' => '\App\Http\Controllers\CategoryController#index']);
Route::get('/tag/{tag}', ['as' => 'tag', 'uses' => '\App\Http\Controllers\TagController#index']);
Read more at https://laravel.com/docs/7.x/routing
The remaining route should do fine, cause you define it last.

Routes are not working properly

When the user accesses "http://proj.test/" instead of getting the homepage I get:
Sorry, the page you are looking for could not be found.
But if the user accesses "http://proj.test/home" it works.
Also when the user accesses "http://proj.test/conference/create" instead of appear the page with the form to create a conference it appears:
View [app] not found. (View: /Users/johnw/projects/proj/resources/views/conferences/create.blade.php)
Do you know where can be the issue? Should be something about the links or routes but I don't know where is the issue.
Links that I'm using
<a class="logo" href="{{route('home')}}">Homepage</a>
Create Conference
Login
Logout
Register
Routes:
Route::group(['prefix' => '', 'middleware' => 'auth'], function(){
Route::post('/conference/store', [
'uses' => 'ConferenceController#store',
'as' => 'conference.store'
]);
Route::get('/conference/create', [
'uses' => 'ConferenceController#create',
'as' => 'conference.create'
]);
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
When the user accesses "http://proj.test/" instead of getting the homepage I get:
Sorry, the page you are looking for could not be found.
You have not defined any route for this URL
You can do
...
Route::get('/', 'HomeController#index');
Route::get('/home', 'HomeController#index')->name('home');
...
Very simple answer - you need a Route::get('/', ...) route.
Add this to your Routes instead:
Route::group(['middleware' => ['guest']], function(){
Route::get('/', 'WelcomeController#index');
// WelcomeController is my own example, yours will differ...
});
This way it'll check to see if there's an auth or not and will redirect accordingly.

LARAVEL: POST and GET routes with same name scrambling link_to_route

I got these routes:
Route::get('prefix/list/{page?}/{size?}', ['as' => 'prefix.list', 'uses' => 'MyController#getList']);
Route::post('prefix/list', ['as' => 'prefix.list', 'uses' => 'MyController#postList']);
When I call link_to_route() like so:
{{ link_to_route('prefix.list', $page, ['page' => $page, 'size' => $size]) }}
It creates this link:
http://my.site/prefix/list?page=5&size=12
But when I remove the post route, it renders correctly this:
http://my.site/prefix/list/5/12
I don't want to change the name of the routes because my system depends on them being the same. How can I solve this?
You could try just changing the order of the routes in your routes file, so that the get one comes last and overrides the post for the purposes of link_to_route().

DaveJamesMiller Breadcrumbs Error - Laravel

I am currently trying to set up breadcrumbs for my Laravel 5 application. Unfortunately, I am currently being presented with this error when I access localhost:8888/auth/login:
ErrorException in
/Users/ben/Sites/laravel/vendor/davejamesmiller/laravel-breadcrumbs/src/CurrentRoute.php
line 29
The current route (GET /auth/login) is not named - please check
routes.php for an "as" parameter
Routes.php:
Route::get('auth/login', 'Auth\AuthController#getLogin',
['as' => 'login', 'uses' => 'Auth/AuthController#getLogin']);
The error is shown with or without the ['as' => 'login', 'uses' => 'Auth/AuthController#getLogin'] addition.
Breadcrumbs.php
Breadcrumbs::register('login', function($breadcrumbs)
{
$breadcrumbs->parent('home');
$breadcrumbs->push('Login', route('login'));
});
Thank you for your help.
I resolved this issue by changing the route to the following:
Route::get('auth/login',
['as' => 'login', 'uses' => 'Auth/AuthController#getLogin']);
You can only declare which controller method you're using once.

Display home page in base url without showing /home (Laravel)

If I type my website url in browser as www.test.com/ then it redirects to www.test.com/home. But I want to see the url in browser as www.test.com only.
routes.php
Route::get('/', array('uses' => 'HomeController#home', 'as' => 'home'));
Route::get('home', array('uses' => 'HomeController#home', 'as' => 'home'));
Try this in your route file:
Route::get('/', 'Admin\HomeController#index');
and remove the extra home route.
See, if that helps.
Route::get('/', array('uses' => 'HomeController#home', 'as' => 'home'));
Keep this route only and remove the next route
It will work as you are expecting then

Resources