Laravel routes error get request - php

Setting up a simple get request seems to throw a http error, this is my code so far:
Link:
<a href="{{ route('account.single', ['id' => $account->id]) }}">
Route:
Route::get('/admin/overview/{id}', [
'uses' => '\App\Http\Controllers\AdminController#getSingle',
'as' => 'account.single',
'middleware' => ['auth', 'admin'],
]);
Shows in the url as - admin/overview?id=5
Controller:
public function getSingle($id)
{
return view('admin.account');
}
This is the error I get - notFoundHttpException in RouteCollection.php line 161:
Note if I remove the /overview/ from the route url so it displays as /admin/{id} it works but the get request doesn't find the id it finds the /overview/ from the url

As mention in Jonathon's comment, the url for your route really should be being displayed as /admin/overview/5 as you've intended and not /admin/overview?id=5.
To identify the problem you'll need to:
Double check you're not seeing cached routes - in the command line run: php artisan route:clear
Check through your routes to make sure you're not hitting another route further up your routes.php file e.g. admin/{some_variable}. You can use php artisan route:list to view the list of routes Laravel is looking for
If you find another route that is causing an issue adjust the order in your routes.php file placing routes with greater specificity above routes with less specificity (example below).
Route::get('/admin/overview/{id}', [
'uses' => '\App\Http\Controllers\AdminController#getSingle',
'as' => 'account.single',
'middleware' => ['auth', 'admin'],
]);
Route::get('/admin/{some_variable}', [
'uses' => '\App\Http\Controllers\AdminController#getSomething',
'as' => 'account.something',
'middleware' => ['auth', 'admin'],
]);

Yours url is incorrect. You should build url like this:
/admin/overview/2
To do it use laravel helper function:
route('account.single', ['id' => 2])

Related

Laravel 8 'Route [admin.test] not defined'

I'm trying to write a new route, but it doesn't work. Even this route is not shown in the route:list. But if i write route:cache, this route is working. It's annoying for each new route, how to solve them ?
Route::group(['prefix' => 'admin', 'middleware' => 'auth', 'as' => 'admin.'], function () {
Route::get('/test', [AdminIndexController::class, 'index'])->name('test');
});
in blade.php
Test
If you want to remove the routes cache , remove this file:
bootstrap/cache/routes.php
After that you can run artisan command
php artisan cache:clear
But There's no problem about not using route cache. It just can make "your route registration up to 100x faster" as noted in the documentation.
Add this line in you Route group:
Route::get('/test/create', [AdminIndexController::class, 'create'])->name('test.create');
Now your route should look like this
Route::group(['prefix' => 'admin', 'middleware' => 'auth', 'as' => 'admin.'], function () {
Route::get('/test', [AdminIndexController::class, 'index'])->name('test');
Route::get('/test/create', [AdminIndexController::class, 'create'])->name('test.create');
});
thank

Laravel add dynamic parameter before route

I have a route like
http://localhost.fbleads.com/{dynamic-value}/auth/facebook-login
It give me error that route not found. Any solution for this
In your routes you could maybe have something like:
Route::get('{dynamic-value}/auth/facebook-login', [
'as' => 'auth.login',
'uses' => 'AuthController#login'
]);
the {dynamic_value} can be anything and be accessible as parameter in your controller method

get route in laravel overriding resource route , How to overcome this

I have the following route in my laravel route file:
Route::get('/{id}' , [
'uses' => 'PagesController#show',
'as' => 'getArticle'
]);
The problem with the above route is , its overriding the below route:
Route::resource('admin', 'adminController');
I want to keep my resource route, but how do i keep my resource ? is there a way around this ??
Modify your route file like this.
Route::resource('admin', 'adminController');
Route::get('/{id}' , [ 'uses' => 'PagesController#show', 'as' => 'getArticle' ]);
Route files executed in the order they are defined.
If you define Route::get('/{id}',.... in the beginning and set your url like http://your-site/admin, the admin section will be considers as the id for the Route::get('/{id}',.... route. So you need to keep it in your mind when you define your route.
just move this route in the end of the web.php file.
Route::get('/{id}' , [
'uses' => 'PagesController#show',
'as' => 'getArticle'
]);
There are two options:
move Route::get('/{id}', ...) after Route::resource(...)
or add a pattern to Route::get() if id is numeric Route::get('/{id}', ...)->where('id', '[0-9]+');

Laravel localize url helper

I need to localize my website. I am using this package: https://github.com/mcamara/laravel-localization
My route group is:
Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
Route::get('/garage', ['as' => 'garage', 'uses' => 'GarageController#garage']);
//OTHER ROUTES
});
And i want to localize link to this route. If i am using
href="{{ route('garage') }}
everything is ok, link looks like "www.example.com/locale/garage".
But if i am using
href="{{ url('/garage') }}
my localization doesn't work, link looks like "www.example.com/garage".
Is there some way to localize links made with URL helper?
LaravelLocalization package includes a middleware object to redirect all non-localized routes to the corresponding "localized".
So, if a user navigates to http://www.example.com/test and the system has this middleware active and 'en' as the current locale for this user, it would redirect him automatically to http://www.example.com/en/test.
To active this functionality go to app/Http/Kernel.php and add these classes in protected $routeMiddleware = [] at the beginning and then in your routes file bring these changes:
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => [ 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath' ]
], function()
{
Route::get('/garage', ['as' => 'garage', 'uses' => 'GarageController#garage']);
//OTHER ROUTES
});
And your problem will be solved.

How to have multiple base routes in laravel 5.2

Consider the following:
Route::get('/', ['as' => 'home.index', 'uses' => 'HomeController#index']);
Route::group(['domain' => 'thechildandthepoet' . env('CONNECTION')], function() {
Route::get('/', ['as' => 'thechildandthepoet.home', 'uses' => 'GameController#index']);
});
When I go to thechildandthepoet.example.local It shows me the contents of
Route::get('/', ['as' => 'home.index', 'uses' => 'HomeController#index']);
completely by passing the fact that I told it which controller to use.
The link looks like: <li>The Child And The Poet</li>
any idea why this doesn't work?
Laravel's router executes the first route that matches given URL.
You don't specify the domain for your first route, therefore it matches all domains. The second route, even though it matches the URL as well, is ignored.
Reorganize your routes.php file, put the routes that specify the domain at the beginning and keep the most generic routes at the end.

Categories