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);
Related
Using vue-tables-2 component in my #vue/cli 4.0.5 app
I see that GET request generated
http://local-ctasks-api.com/api/adminarea/activity-logs-filter?query=&limit=10&ascending=1&page=1&byColumn=0
and I try in Laravel 6 Backend REST API to set route to catch it as :
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'adminarea', 'as' => 'adminarea.'], function ($router) {
Route::get('activity-logs-filter?query={query}&limit={limit}&ascending={ascending}&page={page}&byColumn={column}', 'API\Admin\ActivityLogController#filter');
But I got 404 error,
Is my route invalid ?
UPDATED # 1:
Yes, var with “/api” was unaccesible. I fixed it and running request without “/adminarea”
http://local-ctasks-api.com/api/activity-logs-filter?query=&limit=10&ascending=1&page=1&byColumn=0
I moved route definition out of any block :
Route::get('activity-logs-filter?query={query}&limit={limit}&ascending={ascending}&page={page}&byColumn={column}', 'API\Admin\ActivityLogController#filter');
I got error in browser :
"error": "INCORRECT ROUTE"
with control action defined in app/Http/Controllers/API/Admin/ActivityLogController.php :
public function filter( $query, $limit, $ascending, $page, $column )
{
\Log::info('!!++ filter $this->requestData ::');
\Log::info(print_r( $this->requestData, true ));
Why error ?
Thanks!
I think you forgot put api on prefix
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'api/adminarea', 'as' => 'adminarea.'], function ($router) {
EDIT:
Don't put parameter on route like that, use Request instance
Route::get('activity-logs-filter,'API\Admin\ActivityLogController#filter');
and controller
public function filter(Request $request){
$query = $request->query;
$limit = $request->limit;
$ascending = $request->ascending;
$page = $request->page;
$column = $request->column;
dont forget use Illuminate\Http\Request; on your controller
In the example from the tutorial, it shows up.
Route::group([
'prefix' => 'admin',
'as' => 'admin.'
], function () {}
Can someone tells me what 'as' does? Also, is the dot next to the 'admin' neccessary?
Thank you.
Let's say, for example, that you have this route:
Route::get('admin', [
'as' => 'admin', 'uses' => 'AdminController#index'
]);
By using as you assign custom name to your route. So now, Laravel will allow you to reference said route by using:
$route = route('admin');
So you don't have to build the URL manually over and over again in your code. You don't really need . notation if you only want to call your route admin. If you want a more detailed name of your route, lets say for ex. admin product route, then you use the . notation, like this:
Route::get('admin/product', [
'as' => 'admin.product', 'uses' => 'AdminController#showProduct'
]);
So now, you will be able to call this route by the assigned name:
$route = route('admin.product');
Update:
The previous answer I provided is valid for a single routes. For the route groups, the procedure is very similar. In the route groups you need the . notation when you add a custom name, since you will be referencing another route after that . notation. This will allow you to set a common route name prefix for all routes within the group. So by your example, lets say you have a dashboard route inside your admin route group:
Route::group(['as' => 'admin.'], function () {
Route::get('dashboard', ['as' => 'dashboard', function () {
//Some logic
}]);
});
Now, you will be able to call the dashboard route like this:
$route = route(admin.dashboard);
You can read more about this in Laravel official documentation.
you may specify an as keyword in the route group attribute array, allowing you to set a common route name prefix for all routes within the group.
For Example
Route::group(['as' => 'admin::'], function () {
// Route named "admin::"
});
UseRoute Name like {{route(admin::)}} or route('admin::')
you can use an 'as' as a named route. if you do not prefix your route name in group route than you may add custom route name like this.
Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'roles'], 'roles' => ['2']], function () {
Route::post('/changeProfile', ['uses' => 'UserController#changeProfile',
'as' => 'changeProfile']);
});
I want to have nested resources like that:
Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function ()
{
Route::resource('/articles', 'ArticleController');
Route::group(['prefix' => '/articles', 'as' => 'articles.'], function () {
Route::resource('/types', 'ArticleTypeController');
});
});
But nested route for "article/type" doesn't work, I check my ArticleTypeController outside the "article" route and work.
I really confused, Everybody can help me?
and here is my controller:
class ArticleTypeController extends Controller
{
public function index()
{
$types = ArticleType::all();
return view('manage.articles.types.index')->withtypes($types);
}
}
Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function ()
{
Route::get('articles/types', 'ArticleTypeController#articleTypeMethod');
Route::resource('articles', 'ArticleController');
Route::resource('articles.types', 'ArticleTypeController');
});
for nested resources use articles.types. plural naming is good.
now that manage/articles and manage/articles/1/types will work.
If you want to put a custom route, put it above the resource route if the controller has been used as a resource. see the articles/types [GET] route which maps to ArticleTypeController's articleTypeMethod. now this way http://localhost.com/manage/articles/types should work
here is the 5.1 documentation and it has been removed from documentation of 5.5. but have a look at what Taylor said about it here
it's not recommended on REST to use index function for articles/types, a nested resources index method is used like articles/{id}/types.
for articles/types you need to create a new method.
but if you still want to do it like that. just make it like this
Route::group(['prefix' => 'manage', 'middleware' => ['role:boss']], function ()
{
Route::get('articles/types', 'ArticleTypeController#index');
Route::resource('articles', 'ArticleController');
Route::resource('articles.types', 'ArticleTypeController', ['except' => ['index']]);
});
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.
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
}
}