This is my route
Route::get(
'account-executive/{$id}/edit',
array(
'as' => 'vendor-edit',
'uses' => 'AdminController#updateVendor'
)
);
This is my method
public function updateVendor($id)
{
$vendor = Vendor::findOrFail($id);
return View::make('admin.edit-account-executive');
}
I keep getting NotFoundHttpException. Any ideas as to why?
Your route definition isn't correct.
Route::get('account-executive/{$id}/edit', array('as' => 'vendor-edit','uses' => 'AdminController#updateVendor'));
You don't need a $ sign in definition of route parameter. So you should just write account-executive/{id}/edit.
Try it.
Related
Is there any way to define the name of the route group in laravel 8?
I'm trying to build routes for the sellers to go to the order management site, and below is my routes list in web.php
use App\Http\Controllers\Seller\OrderController;
Route::group(['prefix' => 'seller', 'middleware' => 'auth', 'as' => 'seller.', 'namespace' => 'Seller'], function () {
Route::redirect('/','seller/orders');
Route::resource('/orders', [OrderController::class]);
});
Errors
ErrorException
Array to string conversion
at C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:416
412▕ protected function getResourceAction($resource, $controller, $method, $options)
413▕ {
414▕ $name = $this->getResourceRouteName($resource, $method, $options);
415▕
➜ 416▕ $action = ['as' => $name, 'uses' => $controller.'#'.$method];
417▕
418▕ if (isset($options['middleware'])) {
419▕ $action['middleware'] = $options['middleware'];
420▕ }
1 C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:416
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Array to string conversion", "C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php", ["orders", "index", "orders.index"])
2 C:\wamp64\www\my-project\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:189
Illuminate\Routing\ResourceRegistrar::getResourceAction("orders", "index", [])
Change the following line
Route::resource('/orders', [OrderController::class]);
to
Route::resource('/orders', OrderController::class);
The reason it is giving you that error is because you are passing that `OrderController as an array:
Route::resource('/orders', [OrderController::class]);
You are probably confusing the syntax for regular routes with the syntax for resource controllers.
For resource controllers you have to pass the class name as a string:
Route::resource('/orders', OrderController::class);
this worked for me. SO #Iyathurai Iyngaran
Change the following line
use locationcontroller/OrderController as OrderController;
Route::resource('/orders', OrderController::class);
Bizarre issue, lets see some routes:
Route::get('/admin/races', ['as' => 'races.list', 'uses' => 'RacesController#index']);
Route::get('/admin/races/{race}', ['as' => 'races.race', 'uses' => 'RacesController#show']);
Route::get('/admin/races/create', ['as' => 'races.create', 'uses' => 'RacesController#create']);
Route::get('/admin/races/{race}/edit', ['as' => 'races.edit', 'uses' => 'RacesController#edit']);
Seems normal enough, lets see the controller:
class RacesController extends Controller {
public function index() {
return view('admin.races.list');
}
public function show(GameRace $race) {
return view('admin.races.race', [
'race' => $race,
]);
}
public function create() {
return view('admin.races.manage', [
'race' => null,
]);
}
public function edit(GameRace $race) {
return view('admin.races.manage', [
'race' => $race,
]);
}
}
Seems normal enough. The issue is:
When I go to /admin/races/create I get a 404. The reason being is because, exception:
Illuminate\Database\Eloquent\ModelNotFoundException^ {#851
#model: "App\Flare\Models\GameRace"
#ids: array:1 [
0 => "create"
]
#message: "No query results for model [App\Flare\Models\GameRace] new"
#code: 0
#file: "./vendor/laravel/framework/src/Illuminate/Routing/ImplicitRouteBinding.php"
#line: 47
trace: {
.....
Why is calling:
<li>Create Race</li>
Causing Laravel to take the word create and inject it in as a model? No other route that I have, that is similar does this. For context, here's how we create items:
Route::get('/admin/items/create', ['as' => 'items.create', 'uses' => 'ItemsController#create']);
Same concept, just instead of races its items. So how laravel messing this up?
I have run all the cache clears and route clears and everything. Same issue. Even tests are failing on this. No where am I calling this with a param (especially not one called create) so it should not be assuming there is a param.
It's because you have defined Route::get('/admin/races/{race}' .. first, so it'll hit that route regardless of what the value is. Simply moving the create route before the show route will solve your problem.
Route::get('/admin/races', ['as' => 'races.list', 'uses' => 'RacesController#index']);
Route::get('/admin/races/create', ['as' => 'races.create', 'uses' => 'RacesController#create']);
Route::get('/admin/races/{race}', ['as' => 'races.race', 'uses' => 'RacesController#show']);
Route::get('/admin/races/{race}/edit', ['as' => 'races.edit', 'uses' => 'RacesController#edit']);
That said, you can simplify this a lot more with a simple resource-route.
Route::resource('/admin/races', ['as' => 'races.list', 'uses' => 'RacesController'])->only("index", "show", "create", "edit");
An alternative solution to the accepted answer is to specify conditions for the parameter in the parametrised route. For example if {race} needs to be numeric you can do:
Route::get('/admin/races', ['as' => 'races.list', 'uses' => 'RacesController#index']);
Route::get('/admin/races/{race}', ['as' => 'races.race', 'uses' => 'RacesController#show'])->where('race', '\d+');
Route::get('/admin/races/create', ['as' => 'races.create', 'uses' => 'RacesController#create']);
Route::get('/admin/races/{race}/edit', ['as' => 'races.edit', 'uses' => 'RacesController#edit']);
This is useful in the cases where you can't control the order of the routes (e.g. there's a package registering routes that conflict).
I have config route like this:
Route::get(
'index/{type?}',
[
'as' => 'AAA.index',
'uses' => 'AAA#index',
]
);
Route::any(
'create/{type}',
[
'as' => 'AAA.create',
'uses' => 'AAA#create',
]
);
If I have a string AAA.index, I want to get index/{type?}
If I have a string AAA.create, I want to get create/{type?}
What is the method can do that?
Use route method:
route('AAA.index', $parameter);
See in: https://laravel.com/docs/5.2/helpers#method-route
I'm experiencing routing confusion in laravel 4.
Route::group(['prefix' => 'myProfile', 'before' => 'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
When i write to address bar domain.app/myProfile it runs second route and runs ProfileController#index...
Thanks.
Looks like correct behaviour. To access first route you would have to type something like domain.app/myProfile/FooUser. You didn't specify / route in myProfile route group, so it cannot match it and uses second one.
Breaking down your routes:
1)
Route::get('/{username}', [
'as' => 'show-profile',
'uses' => 'ProfileController#index'
]);
Use /example URI to access the above route.
2)
Route::group(['prefix' => 'myProfile', 'before' =>'auth|inGroup:Model|isMe'], function()
{
Route::get('/{username}', function(){
echo 'hello';
});
});
Use /myProfile/example URI to access the above route.
Your application is working as expected.
So I have these 2 routes:
/*
* Account Activate (GET)
*/
Route::get('/account/activate/{code}', array(
'as' => 'account-activate',
'uses' => 'AccountController#getActivate'
));
/*
* Account Activate EMPTY-CODE (GET)
*/
Route::get('/account/activate/', array(
'as' => 'account-activate',
'uses' => 'AccountController#getActivateEmpty'
));
They are meant to pick up the code from a URL like this: http://localhost:81/account/activate?duYCzo5TEhmRFMBnDEJUSY4EO81EBCJlOyccVBNxpNPksBg6bJJrvUVV4XnX
Unfortunately as you can see the URL isn't activate/code it's activate?code.
This is the code creating the URL (its in a Mail function):
'link' => URL::route('account-recover-code', $code)
What can I change to make sure my route works as intended?
You may try this:
URL::route('account-activate', array('code' => $code));
Also use only one route declaration and make the {code} optional using ? like this:
Route::get('/account/activate/{code?}', array(
'as' => 'account-activate',
'uses' => 'AccountController#getActivate'
));
The URL::route() method expects the route name, which is as value in the route declaration and in {code?} the ? made the parameter optional so if you pass a code to your route then you can pass it as array('code' => $code) and if you dont want to pass the parameter then just use following code to generate the URL:
URL::route('account-activate');
In this case your method should be like:
public function getActivate($code = NULL)
{
if(!is_null($code)) {
// $code is available
}
else {
// $code is not passed
}
}