Laravel too few arguments to function,0 passed andexactly 2 expected - php

I am trying to pass the 2 parameters to my index method in the controller but it does not pass and says 0 have been sent. I have checked if the variables can be shown on the view which it can so that isn't the problem.
In my blade file I have the following <a tag:
Is the circumstance: <strong>{{$variables->circumstance}}</strong> true?
web.php route:
Route::resource('attack', AttackController::class)->middleware('auth');
Controller:
public function index($debate, $argument){//}
Error message:
Too few arguments to function App\Http\Controllers\AttackController::index(), 0 passed in C:\Users\hakar\argupedia\Argupedia\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 2 expected

Blade File
Is the circumstance: <strong>{{$variables->circumstance}}</strong> true?
Route File
Route::group(['middleware' => 'auth'], function () {
Route::get('/attack/{debate}/{argument}', 'AttackController#schemes')->name('search');
});
Controller
public function schemes($debate, $argument){
dd($debate, $argument);
}

If you're only using one method in a controller, there is no need for a resource route, Better way would be to create a single route like this:
Route::get('attack/{debate}/{argument}', 'AttackController#index')->middleware('auth')->name('attack.index');
The arguments in the curly brackets are called route parameters. Now, in order to pass this parameters, you need to pass them like key => value arrays
Is the circumstance: <strong>{{$variables->circumstance}}</strong> true?
Now, you have access to these parameters in your controller:
public function index($debate, $argument){
dd($debate, $argument);
}
If both parameters are not required here, you can use optional parameters in your routes, by using ? mark, like this:
Route::get('attack/{debate?}/{argument?}', 'AttackController#index')->middleware('auth');
You can now pass only one parameter to your route, without crashing your application.

Related

Route function meaning

I am having some difficulty understanding what this code is doing. could someone explain it to me? I saw some people using it to redirect user to another page with it but I don't understand this part here "['id'=>$data3->id]) ".
Here is the full code: (from view page)
<a href="{!! route('user.upload.image', ['id'=>$data3->id]) !!}">
Controller (how data3 is being passed to view):
public function getInfo($id) {
$data3=UserImage::where('user_id',$id)->get();
return view('view',compact('data3'));
Route:
Route::get('/userUpload/{user}/create1','CreateController#create1')->name('user.upload.iamge');
Route::get('user/show/{id}','HomeController#getInfo')->name("user.show");
create1 controller:
public function create1(personal_info $user){
return view('create1')->withUser($user);
}
Based on your routes
Route::get('/userUpload/{user}/create1','CreateController#create1')->name('user.upload.iamge');
Route::get('user/show/{id}','HomeController#getInfo')->name("user.show");
The first route has a parameter user which must be passed to it anytime the route is called.
The second one also has an id parameter which must also be passed to it.
Passing the parameter values to the routes can be done in many ways. Eg.
By using the route name:
<a href="{!! route('user.upload.image', ['user'=>$data3->id]) !!}">
This method requires you to pass all the parameters as an array with the parameter name as the key of the array.
You can also call the route like:
<a href="/userUpload/{$data3->id}/create1">
Which requires nothing since the parameter has been hardcoded into the url.
Any time you accept parameters in your route, to pass them to your controller or route function, the must be listed on the order in which they are arranged.
So your getInfo passes the id parameter it received from the route to the controller
public function getInfo($id) {
$data3=UserImage::where('user_id',$id)->get();
return view('view',compact('data3'));
}

Proper way to pass a hard-coded value from route to controller (Laravel)?

I have a PagesController with one action: view.
This action accepts a page argument.
What I want to achieve:
Have a routes example.com/about and example.com/foobar.
When one of this routes is triggered, pass a value predefined in routes file to PagesController#view.
In my routes file:
Route::get('about', function () {
return App::make('App\Http\Controllers\PagesController')->view('about');
})->name('aboutPage');
Route::get('foobar', function () {
return App::make('App\Http\Controllers\PagesController')->view('foobar');
})->name('foobarPage');
It works as expected, but I want to know is there a better and more proper way to achieve the same functionality?
Pass your pages as route parameter:
Route::get('{page}', 'PagesController#view');
//controller
public function view($page)
{
//$page is your value passed by route;
return view($page);
}
So you just want an argument to your action. You can use optional parameters if that argument can be empty. You can read more about it here.
Route::get('{argument?}', 'PagesController#view')->name('page');
And in your PagesController:
public function view($argument = 'default') {
// Your logic
}
The accepted answer is what you want based on what you are doing.
If you really wanted a hardcoded value you can use the 'actions' array part of the route if you wanted.
Route::get('something', ['uses' => 'Controller#page', 'page' => 'something']);
public function page(Request $request)
{
$page = $request->route()->getAction()['page'];
...
}
asklagbox - blog - random tips and tricks
If you don't need the names of the routes like in your example
->name('foobarPage');
you can use something like this
Route::get('{page_name}','PagesController#view')->where('page_name', '(about)|(foobar)');
This will accept only the values passed in the regular expression for the page_name parameter. Other routes will throw a 404 error. I should mention that this technique seems to be valid for applications with one level of url nesting only and should NOT be used as a pattern.
From what I can see above if all you are doing is showing the correct view I would go for
Route::get('{page}', function($page)
{
if (view()->exists($page)) {
return view($page);
}
return abort(404);
});
This prevents you even needing a method in your controller.

Laravel getting {id} get parameter in middleware

The problem: I can't get the {id} parameter to be 'found' in the Middleware. My middleware needs to get the {id} param, in order to verify if the Auth::user() is the owner of the specified group.
Request example: groups/admin/open/4 --> group 4 will be opened.
What I have:
Route:
Route::post('groups/admin/open/{id}', 'GroupController#opengroup')->middleware(['auth','owner']);
My middleware ('owner') is still empty.
What I tried:
1. Adding an $id parameter in the function (like you do in Controllers), like so:
public function handle($request, /* Added -> */ $id, Closure $next)
{
dd(Input::get('id'));
return $next($request);
}
This returns an error:
Argument 3 passed to App\Http\Middleware\RedirectIfNotOwner::handle() must be an instance of Closure, none given
Different placement of the $id, at the end, results in the error:
Missing argument 3 for App\Http\Middleware\RedirectIfNotOwner::handle()
What I have tried 2: I have thought about is to change my url like this
Request example: groups/admin/open?groupparam=4
And use
Input::get('groupparam');
But this forces me to make changes to my controllers etc. Still this is an option.
Reason for asking: moreover I believe Laravel has the capability to retrieve {id} params in Middleware too, beautifully. I just don't know how.
Can you help me?
Thanks,
Eltyer
You can easily get route parameters in your route middleware with:
$id = $request->route('id');

Providing the function value when using Laravel routes 'uses'

Normally just pass the uses method to the URL but now need to provide a value for the function.
So I have a ajax call that needs to use the function:
public function getPostData($id) {
}
Which is in my PostController.
In the past my routes for this would be e.g.:
Route::post('createpostaction', array('uses' => 'PostController#create'));
How do I now pass a variable into the function?
Route::post('getPostData', array('uses' => 'PostController#getPostData));
Thanks.
Route parameters will automatically get passed to the uses function.
Route::post('get-post-data/{id}', array('uses' => 'PostController#getPostData'));
This means that you now have to call the URL like this:
get-post-data/123
And the controller will receive 123 as the first parameter.
If you add a second route parameter
Route::post('get-post-data/{id}/{foo}', array('uses' => 'PostController#getPostData'));
You receive it as second parameter
public function getPostData($id, $foo){}
and so on...
Note The names of route parameters and function arguments don't have to match. Only the order of them is important.

Laravel passes a question mark in route()

My route function in Laravel adds a question mark (?), instead of a slash (/)
route('servers.index', 321); // http://domain/public_html/server?321
I want it to return http://domain.com/public_html/clientarea/server/321
Routes:
Route::group(['prefix' => 'clientarea'], function()
{
Route::get('/', 'UsersController#index');
Route::get('server/{id}', 'ServersController#index');
});
Route::resource('users', 'UsersController');
Route::resource('servers', 'ServersController');
You should look into how the route() function is supposed to work. For example, you should name the route if you plan to reference it with route(). Once you have it named, you must make sure you're passing in the parameters correctly as Lukas said.
If Laravel can't find matching parameters defined for the route, it will default to making the paramaters a part of the query string. Since the route doesn't exist, it won't be able to find matching parameters to what you're passing in.
Take a look at the docs: http://laravel.com/docs/4.2/routing#named-routes
The route function expects an array for parameters. You can either pass the values by name of the parameter or by order
route('servers.index', array(321));
or this (assuming the parameter is called id
route('servers.index', array('id' => 321));
from the laravel forums
$url = URL::route('welcome') . '#hash';
return Redirect::to($url); // domain.com/welcome#hash
http://laravel.io/forum/02-07-2014-how-to-append-hashtag-to-end-of-url-with-redirect

Categories