I have the following routes:
use NexCast\Domain\NiveisServico\NiveisServicoController;
Route::group(['prefix' => 'niveis-servico', 'name' => 'niveis-servico.'], function () {
Route::get('/', [NiveisServicoController::class, 'getData'])->name('get');
Route::post('/', [NiveisServicoController::class, 'saveData'])->name('save');
Route::delete('/', [NiveisServicoController::class, 'deleteData'])->name('delete');
});
However I am receiving the following error:
Type error: ReflectionFunction::__construct() expects parameter 1 to be string, array given
What am I doing wrong?
The error you're getting happens because you're passing an array as the second parameter to the Route:: methods inside the group, but it expects a string instead. This is an example of what they should look like using your code:
Route::get('/', 'NiveisServicoController#getData')->name('get');
You can find more information about routes in the docs for the Laravel 5.1 version.
Related
My laravel Project Route invalid range in character class prolem please help me?
Route::get('{path}','HomeController#index')->where( 'path', '([A-z]+)?' )
Not solve
eRoute::group(['namespace' => 'Post'], function ($router) {
$router->pattern('id', '[0-9]+');
// $router->pattern('slug', '.*');
$router->pattern('slug', '^(?=.*)((?!\/).)*$');
// SingleStep Post creation
Route::group(['namespace' => 'CreateOrEdit\SingleStep'], function ($router) {
Route::get('create', 'CreateController#getForm');
Route::post('create', 'CreateController#postForm');
Route::get('create/finish', 'CreateController#finish');
I don't know what the intended routing pattern is supposed to be, but I speculate that it does not supports lookarounds. So instead of this:
$router->pattern('slug', '^(?=.*)((?!\/).)*$')
Try this:
$router->pattern('slug', '^[^/]*$');
In previous versions of Laravel I was using something like this in controller in show function
Route::resource( 'our-project', 'ProjectController' );
public function show( Project $project ) {
return view( 'portalComponents.projects.projectDetails', compact( 'project' ) );
}
I was trying the same in laravel 5.8 but the $project attributes comes empty.
Route model binding won't work for our-project/1 because laravel can't infer the model. It tries to bind the our-project placeholder to a variable that has the name name in the show method. That argument doesn't exist. Because if this the $project variable stays empty.
the following resource would work:
Route::resource( 'projects', 'ProjectController' );
because this uses the project placeholder in routes. Check the output from php artisan route:list
It is also possible to have the same resource with different prefixes:
Route::resource('projects', 'ProjectController');
Route::group(['prefix' => 'admin'], function () {
Route::resource('projects', 'ProjectController');
});
the first one is /projects/1 and the second one is /admin/projects/
For the sake of completness and as alternative to #MaartenDev right answer, if you want to define the name of the parameter used with a resource route you can use the parameters() function, i.e.:
Route::resource( 'our-project', 'ProjectController' )
->parameters(['our-project' => 'project']);
I write api for mobile, since I have more than one version, I don't want to copy-paste the same routes, hence I decided to do something like this:
Route::model('callRequestNote', CallRequestNote::class);
Route::group([
'prefix' => 'api/v{version}/mobile',
'where' => ['version' => '[1|2]'],
], function () {
Route::delete('/notes/{callRequestNote}', MobileNotesController::class . '#destroy');
// other duplicated routes
});
public function destroy($version, CallRequestNote $callRequestNote)
{
//
}
I use version parameter only in BeforeMiddleware, hence I don't need it in controller. My question is how to replace those parameters and make CallRequestNote as a first argument and get rid of $version?
Using Optional Parameters
When i use optional parameters in Laravel 5.4, i get the error internal error: failed to retrieve the default value.
Here is My routes:
Route::group(['middleware' => ['auth','role:SUPER_ADMIN'],'prefix' => 'SWSM-Dashboard'], function () {
Route::get('/', 'HomeController#eventDashboard');
Route::group(['prefix' => '/providers'],function (){
Route::get('create/{providerData?}','ProvidersController#create');
Route::get('delete/{providerId?}','ProvidersController#destroy');
Route::get('/','ProvidersController#index');
});
});
Here is my controller function
public function create($providerData){ dd('Hello'); }
My middleware is just the standard Auth out of the box with Laravel so i'm surprised that this is not working.
I am using php 5.3 so type hinting is not available to me, so i would not normally use it.
The problem is resolved by doing the following to the optional parameter you want to use inside your controller.
public function create($providersData = null)
{
dd($providersData);
}
Now when the route is hit with ..../create/ it will die and dump null but when the route is hit with ..../create/1 the providersData parameter will be set to 1 and die and dump.
When I use Route::controller() it will automatically generate a route like: GET|HEAD|POST|PUT|PATCH|DELETE resource/{_missing}.
this will make conflict if I have route like resource/{id}/somethingElse.
Sample code
<?php
Route::controller('page', 'PageController');
Route::Group(['prefix' => 'page/{id}/comments'], function() {
Route::get('/', 'CommentController#index');
Route::post('/', 'CommentController#create'); // <-- this will not work sometimes I don't know why
});
The line I highlighted throws NotFoundHttpException with Controller method not found. message.
Is there anyway to remove that route containing {_missing} parameter?