Laravel naming route and a href - php

Route::get('/products/{page?}', 'ProductController#index')->name('products');
Route::get('/products/{category}/{page?}', 'ProductController#indexCategory')->name('category.products');
I have two route like this.
When i call the second route like :
<a href="{!! route('category.products', ['category' => $category['translation']['en']['slug']]) !!}" class="list-group-item active">
it calls #index method instead of #indexCategory.
idk why?
I want to make
GO TO INDEX METHOD
/products, /products/2, products/3
GO TO INDEXCATEGORY METHOD
/products/new-collection, /products/new-collection/2,
/products/new-collection/3

I think you need to update the route like this.
Route::get('/products/{page?}', 'ProductController#index')->name('products');
Route::get('/products-category/{category}/{page?}', 'ProductController#indexCategory')->name('category.products');
Because naming convention conflicts your route. so make sure route URL is unique.
And then call your route like:
<a href="{!! route('category.products', ['category' => $category['translation']['en']['slug']]) !!}" class="list-group-item active">
OR
If you would like to do not change the route URL then make sure your second perameter required. not optional.

Related

route() returns different way in two route

I made two resource routes and tied them into a group.
Route::group(['middleware' => 'auth'], function () {
Route::resource('user/posts', PostController::class)->name('show', 'user.posts');
Route::resource('admin/posts', PostController::class)->middleware('is_admin')->name('show', 'admin.posts');
});
The href tag below code only works in admin, which turns to '/admin/posts'. but when a not admin user is logined, It points '/posts' not '/user/posts'/.
<x-jet-nav-link href={{route('posts')}}" :active="request()->routeIs('posts')">
{{ __('Posts') }}
</x-jet-nav-link>
So I had to change the code like below.
<x-jet-nav-link href="{{ route(auth()->user()->isAdmin?'admin.posts':'user.posts', '') }}" :active="request()->routeIs('posts')">
{{ __('Posts') }}
</x-jet-nav-link>
what did I miss on the first method?
Additionally :active= is not working. How can I fix that?
Can you run php artisan route:list and post the output?
And for the second part with the href for the link, I think you need to specify the route like posts.index or posts.show and for the isActive try: :active="request()->routeIs('posts.*')"

Laravel 5.6 - A shared name for route resources

While creating an admin menu, i was wondering how to set active class item. The menu item have to stay active if the controller is showing, editing or doing something else.
sidebar.blade.php
<li class="nav-item">
<a class="nav-link {{ (Route::current()->getName() == 'posts' ? 'active' : '') }}" href="/admin/posts">Posts</a>
</li>
routes/web.php
// POSTS
Route::resource('/admin/posts', 'Admin\PostController');
How to set a shared name for all resources (index, create, show, etc.)?
I was hoping to do something like this but...
Route::resource('/admin/posts', 'Admin\PostController')->name('posts');
Thanks
Naming Resource Routes
By default, all resource controller actions have a route name; however, you can override these names by passing a names array with your options:
Route::resource('photos', 'PhotoController')->names([
'create' => 'photos.build'
]);
You can find more options in the documentation.
work in laravel 9 try in other version
Route::resource('vente', VenteController::class,
['names'=>['index'=>'vente.index']]);
call it in menu
vente

Route not defined php laravel5.5

I got an error : Route [admin/news] not defined I try to use #component in my create.blade.php
In my Controller I declare variable
public $route = 'admin/news' ;
In web.php
Route::post('admin/news/create', 'Admin\NewsController#store');
Route::resource('admin/news', 'Admin\NewsController');
In my html this return right Url
<a class="btn btn-success" href="{{ asset($route.'/create') }}">add</a>
<a class="btn btn-success" href="{{ route('news.create') }}">add</a>
I check my route by using php artisan r:l
It has a news.create
I try to use other routes both of these work fine not sure what's wrong with my create route
route('news.edit',$t->id)
route('news.destroy',$t->id)
the problem is in my create.blade.php I try to use #component by this
#component('layouts.submitform',
['id'=>'create','method'=> isset($edit) ? 'PUT' : 'POST' ,'action'=> isset($data->id) ? asset($route.'/'.$data->id) : route($route)]
)
You have $route set to admin/news. You say you want to go to the create page. You then say that the route is named news.create. So use news.create as the name when referencing it with the helper. Set $route to news.create.
You seem to want to use a URI and a route name. You have to decide which one you are going for.
Laravel Docs - Routing - Named Routes
Laravel Docs - Helpers - Url Helpers - route

Laravel 4 User Profile Link

I currently have a route that looks like this:
Route::get('/profile/{username}',array(
'as' => 'profile',
'uses' => 'ProfileController#getProfile'
));
The idea is that a link, like http://www.website-here.com/profile/johnnyappleseed, containing a username will do a search for the username in the database and return the profile that matches based on the username in the URL. How would I create a link to the logged in User's profile using the same route. Currently I have
<li>Profile</li>
But that seems incorrect and that there should be a way to do it using some of Laravels helpers.
You can use the route helper and pass the route name and an array of parameters needed:
<li>
<a href="{{ route('profile', [Auth::user()->username]) }}">
Profile
</a>
</li>
If you want to go all the way and use even more Laravel helpers, you can use the HTML facade and generate the entire link tag in one line:
<li>
{{ HTML::link(route('profile', [Auth::user()->username]), 'Profile') }}
<li>

Diffrence between Laravel Routing Method

what is the difference between both routing ? can anyone explain ?
Route::get('login', 'webcontroller#login');
Route::get('login', array('as' => 'login','uses'=>'webcontroller#login'));
Well. There is the flexibility of Route object (i think it belongs to Symfony)
In the first statement, you explicitly say that you what controller's action a certain address should trigger (in your case it is 'login' which triggers login() of WebController).
In the second statement, you can add an "array" of settings for the controller's method, which, in your case you have specified a name. "login", which is the name of your Route::get() rule for the address "/login", could be used any where in the system without you explicitly specifying any controller or url which gives you the ability to change whatever you like in the future, as long as you are consistent with your names.
You set a route:
Route::get("login", array('as'=>'login', 'uses'=>'LoginController#Login');
Then you can use it like:
$url = URL::route('profile');
Whil you are still able to change the url of your route:
Route::get("user/login", ...);
Without the need to change its uses of "name" within your project.
You can read about it on Laravel's official documentation:
http://laravel.com/docs/4.2/routing#named-routes
In the number 2, you usea an alias , is easy for call de route in the code:
example:
<a href=" {{ route('user.list') }} ">
< span class="glyphicons glyphicons-link"></span>
<span class="sidebar-title">Link</span>
</a>

Categories