Laravel add dynamic parameter before route - php

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

Related

Sorry the page you're looking could not be found. Laravel 5.5

I have laravel 5.5. Everything is working fine in it. I can access all routes.
But somehow I cannot access the route "/interest/{$id}".
This is my route.
<?php
Route::get('/interest/{$id}', [
'uses' => 'IdeaController#createInterest'
])->name('interest.create')->middleware('auth');
I Can access every route accept this one. I hope this helps.
Remove the dollar sign from your route parameter:
Route::get('/interest/{id}', [
'uses' => 'IdeaController#createInterest'
])->name('interest.create')->middleware('auth');
Route::get('/interest/{id}', [
'uses' => 'IdeaController#createInterest'
])->name('interest.create')->middleware('auth');
//in controller use method
public function createInterest($id){
}

Laravel passing variables inside route accessing a named route

I am trying to pass 3 variables values inside an html link that uses route to access a named routed with this code:
{{$nomos->name}}
also the route function is
Route::get('gegonota/{gid?}/{cid?}/{nid?}', ['uses' => 'GegonosController#index', 'as' => 'gegonota']);
but it doesnt work right.
How can i do it.
thanks
you can try this.
{{$nomos->name}}
Just remove ? from your routes params so it will be :
Route::get('gegonota/{gid}/{cid}/{nid}', [
'uses' => 'GegonosController#index',
'as' => 'gegonota'
]);
if you want to use ? you need to provide a default value check the docs
and if you use Laravel v5.4 you need to change your route to :
Route::get('gegonota/{gid}/{cid}/{nid}', [
'uses' => 'GegonosController#index'
])->name('gegonota');

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 routes error get request

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])

laravel routes not working for required parameters

I have one route in my routes.php like
Route::get('{subcat}', array(
'uses' => 'frontend\homeController#uploadAd'
))
And I found that the above route is effecting my userlogout route which is like this.
Route::get('userlogout', array(
'uses' => 'frontend\homeController#userlogout'
));
I am getting the {subcat} route parameter from the blade view like
subcategoryname
If I include my {subcat} route in routes.php, userlogout route does not work but, commenting out my {subcat} route or even modifying that route like:
Route::get('something/{subcat}', array(
'uses' => 'frontend\homeController#uploadAd'
))
enable user to log out from the system.
What am I doing wrong here? Is there anything that I can't specify only route parameter as my route name?
I think you can fix this by defining the logout route after the subcat route like this:
Route::get('{subcat}', array(
'uses' => 'frontend\homeController#uploadAd'
));
Route::get('userlogout', array(
'uses' => 'frontend\homeController#userlogout'
));
This is happening because the first route will match any url with a single parameter. As I know the order of defining routes is important in such cases.

Categories