Providing the function value when using Laravel routes 'uses' - php

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.

Related

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

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.

Alias for a route with a fixed parameter value

I have this route:
Route::get('/MyModel/{id}', 'MyController#show');
The method show() accepts a parameter called id and I want to setup an alias for /MyModel/1 so it's accesible from /MyCustomURL.
I already tried a few combinations, like:
Route::get('/MyCustomURL', ['uses' => 'MyController#show', 'id' => 1]);
But I keep getting missing required argument error for method show().
Is there a clean way to achieve this in Laravel?
In Laravel 5.4 (or maybe earlier) you can use defaults function in your routes file.
Here is example:
Route::get('/alias', 'MyController#show')->defaults('id', 1);
In this case you don't need to add additional method in your controller.
In same controller (in your case MyController ?) you should create one new method:
public function showAliased()
{
return $this->show(1);
}
and now you can define your aliased route like so:
Route::get('/MyCustomURL', 'MyController#showAliased');
define your route like this:
you can use "as" to give your route any name that you need.
Route::get('/MyModel/{id}' , [
'as'=>'Camilo.model.show',
'uses' => 'MyController#show' ,
]) ;
now if you want to access this route, you can generate url for it, based on its name like this:
route('Camilo.model.show', ['id' =>1]) ;
Route::get('MyModel/{id}', 'MyController#show');
not
Route::get('/MyModel/{id}', 'MyController#show');
Good Luck!

How to do a simple redirect in Laravel?

I have a function in Laravel. At the end I want to redirect to another function. How do I do that in Laravel?
I tried something like:
return redirect()->route('listofclubs');
It doesn't work.
The route for "listofclubs" is:
Route::get("listofclubs","Clubs#listofclubs");
If you want to use the route path you need to use the to method:
return redirect()->to('listofclubs');
If you want to use the route method you need to pass a route name, which means you need to add a name to the route definition. So if modify your route to have a name like so:
// The `as` attribute defines the route name
Route::get('listofclubs', ['as' => 'listofclubs', 'uses' => 'Clubs#listofclubs']);
Then you can use:
return redirect()->route('listofclubs');
You can read more about named routes in the Laravel HTTP Routing Documentation and more about redirects in the Redirector class API Documentation where you can see the available methods and what parameters each of them accepts.
Simply name your route:
Route::get('listofclubs',[
'uses' => 'Clubs#listofclubs',
'as' => 'listofclubs'
]);
Then later
return redirect()->route('listofclubs');
One method is to follow the solution provided by others. The other method would be, since you intend to call the function directly then you can use
return redirect()->action('Clubs#listofclubs');
Then in route file
Route::get("listofclubs","Clubs#listofclubs");
Laravel will automatically redirect to /listofclubs.

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

Laravel beforeFilter in controller on POST request throws non-object exception

I try to set some specific filter on all controller methods with:
public function __construct() {
$this->beforeFilter(function(){
//whathever
});
}
and it's working well on normal GET methods, problem occures when there is some POST method:
Route::post('settings/menu-order/{direction}', array(
'as' => 'setting.menu-order.move',
'uses' => function($direction) {
$controller = new CMSSettingsController();
return $controller->doMoveMenu($direction);
}));
after click in a button which send POST with $direction, I'v got
Call to a member function filter() on a non-object
in vendor/laravel/framework/src/Illuminate/Routing/Controller.php
protected function registerClosureFilter(Closure $filter)
{
$this->getFilterer()->filter($name = spl_object_hash($filter), $filter);
return $name;
}
If I use already registred filter it's working, so what's going on?
I have few controllers which need specific function todo before running controller methods, so I can't make global universal filter. Is there any other good solution?
The problem could be that you are calling the controller action directly instead of letting the Router do it for you. When the router tries to apply the filters, instead of applying them on the controller, it ends up attempting to apply them on the output of the doMoveMenu action - which, of course, is not a Controller object and has no method filter.
Instead, your route should look like this:
Route::post('settings/menu-order/{direction}', array(
'as' => 'setting.menu-order.move',
'uses' => 'CMSSettingsController#doMoveMenu'));
The reason you don't need to do the method call manually is that since your Route has a parameter in it and your method accepts a parameter, the Router will automatically pass the parameter into the action method. Additionally, since you are providing a method name as the uses value, Laravel knows that it has to instantiate the Controller and run the filters.

Categories