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]+');
Related
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
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])
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.
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.
When defining a route in Laravel 4 is it possible to define multiple URI paths within the same route?
presently i do the following:
Route::get('/', 'DashboardController#index');
Route::get('/dashboard', array('as' => 'dashboard', 'uses' => 'v1\DashboardController#index'));
but this defeats my purpose, i would like to do something like
Route::get('/, /dashboard', array('as' => 'dashboard', 'uses' => 'DashboardController#index'));
I believe you need to use an optional parameter with a regular expression:
Route::get('/{name}', array(
'as' => 'dashboard',
'uses' => 'DashboardController#index')
)->where('name', '(dashboard)?');
* Assuming you want to route to the same controller which is not entirely clear from the question.
* The current accepted answer matches everything not just / OR /dashboard.
I find it interesting for curiosity sake to attempt to solve this question posted by #Alex as a comment under #graemec's answer to post a solution that works:
Route::get('/{name}', [
'as' => 'dashboard',
'uses' => 'DashboardController#index'
]
)->where('name', 'home|dashboard|'); //add as many as possible separated by |
Because the second argument of where() expects regular expressions so we can assign it to match exactly any of those separated by | so my initial thought of proposing a whereIn() into Laravel route is resolved by this solution.
PS:This example is tested on Laravel 5.4.30
Hope someone finds it useful
If I understand your question right I'd say:
Use Route Prefixing: http://laravel.com/docs/routing#route-prefixing
Or (Optional) Route Parameters: http://laravel.com/docs/routing#route-parameters
So for example:
Route::group(array('prefix' => '/'), function() { Route::get('dashboard', 'DashboardController#index'); });
OR
Route::get('/{dashboard?}', array('as' => 'dashboard', 'uses' => 'DashboardController#index'));