I know this is common to ask, but I have the trouble of the correct way of routing my URL using href
I've tried this
<a href="{{ route('staff.emvvalidationdetails.emvcard',emv.evd_id)}}">
but it returns me an error
Use of undefined constant emv - assumed 'emv' (this will throw an Error in a future version of PHP)
Routes
Route::get('emvvalidationdetailsemvcard', 'EmvvalidationdetailsController#emvcard')->name('staff.emvvalidationdetails.emvcard');
Controller
public function emvcard(Request $request, GetEmvDetailsByEvdId $getEmvDetailsByEvdId)
{
$records = $getEmvDetailsByEvdId->execute($request->evd_id);
// return response()->json($records);
$breadcrumbs_main = 'Validated Accounts';
$bradcrumbs_details = '-';
return view('staff.emvvalidationdetails.update_card',['data_object' => $records],compact('breadcrumbs_main', 'breadcrumbs_details'));
}
you can see here Laravel route that the route() function accepts 3 parameters, the second parameter you are providing is neither a string nor php-variable so that is why PHP assuming that you might have provided a PHP Constant as a parameter in second argument.
If you have a variable named $emv, then you can do something like this:
If $emv is an object: route('staff.emvvalidationdetails.emvcard',$emv->evd_id)
if $emv is an array: route('staff.emvvalidationdetails.emvcard',$emv['evd_id'])
Make sure you provide appropriate parameters.
Related
I have a problem with Laravel 5, and to be precise, I can't find the solution for it.
In C# (ASP.NET MVC) it's easy to solve.
For example, I have these routes (I'll just type the route content, and the function header, for the sake of simplicity)
/{category}/Page{page}
/Page{page}
/{category}
The function is defined inside Product controller.
function header looks like this:
public function list($page = 1, $category = null)
the problem is, whenever I enter just one argument, it doesn't send the value for the parameter by the name I set in the route, but rather, it pushes values by function parameter order.
So, when I open /Page1, it works properly, value of 1 is sent to $page variable,
but when I access /Golf(made up on the spot), it also sends the value to the $page variable.
Any possible idea how to avoid this, or do I really need to make different functions to handle these cases?
In C#, it properly sends the value, and keeps the default value for undefined parameter.
Hope you have an answer for me.
Thank you in advance and have a nice day :)
So, as you've seen the parameters are passed to the function in order, not by name.
To achieve what you want, you can access these route parameters from within your function by type hinting the request object to it like this:
class ProductController extends Controller
{
function list(Request $request){ # <----------- don't pass the params, just the request object
$page = $request->route('page'); # <--- Then access by name
$category = $request->route('category');
dd("Page: $page | Category: $category");
}
}
Then of course you would set all 3 of your routes to hit that same controller method:
Route::get('/{category}/Page{page}', 'ProductController#list');
Route::get('/Page{page}', 'ProductController#list');
Route::get('/{category}', 'ProductController#list');
Hope this helps..!
if you want to get the parameters in your controller, you can use this:
public function list() {
$params = $this->getRouter()->getCurrentRoute()->parameters();
}
for /aaa/Page3, the $params would be array(category => 'aaa', page => '3')
for /Page3, the $params would be array(page => '3')
for /aaa, the $params would be array(category => 'aaa')
I have the following code in my anchor tag in my laravel application:
Submited Quality Check
My route is set to run the following method when clicked on the above link:
public function showQualityResult($qualityData) {
return $qualityData;
// return view('quality-result' , compact($qualityData));
}
Now when i click on the link, i get the following error:
Why am i getting a Missing argument 1 error when i clearly am passing data as a parameter in the tag like so below:
Submited Quality Check
Why is the parameter not being passed to the method ?
Your route from your previous question:
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult');
To pass the parameter, you should define it. So, it'll look like this:
Route::get('showQualityResult/{data}', 'QualityCheckController#showQualityResult');
And showQualityResult() method should accept it:
public function showQualityResult($data)
{
....
I'd recommend you to read the docs to understand how it works.
I need to get variable "type"
Route::model('type', \App\Models\Document::class, function($type) {
return (new \App\Models\ShareFactory($type));
});
than return object and use it in other bind
Route::model('key', \App\Models\Document::class, function($key) {
return $objectFromFactory::where('share_key', $key)->first();
});
And after all I need to set controller that will process request
Route::get('share/{type}/{key}', 'ProcessShareController#share');
Is it possible? Or I'm trying to code in a wrong way?
It is possible, although solution is not future-proof. This will also work only if in your resolution logic you are using route parameters that appear in the route before the parameter currently resolved.
When route parameters are resolved by Router, they are processed in the same order as they are defined in the path. Each resolved parameter is added to route parameters list.
You can access the list of already resolved parameters by calling
$parameters = Route::getCurrentRoute()->parameters();
You will see all route parameters there even if some have not yet been resolved. Before resolution you will see the string from the URL as their value.
So, in your case, you need the following:
Route::model('type', \App\Models\Document::class, function($type) {
return (new \App\Models\ShareFactory($type));
});
Route::model('key', \App\Models\Document::class, function($key) {
$parameters = Route::getCurrentRoute()->parameters();
$objectFromFactory = $parameters['type'];
return $objectFromFactory::where('share_key', $key)->first();
});
Warning:
Remember that key parameter will always be resolved with above logic, even if there is no type parameter in the route. This might lead to errors, as you always have to make sure that if you define one parameter, you also define the other.
As mentioned, this solution is not future-proof. The logic here relies on the assumption, that route parameters are always processed in the order they are defined. Although I don't see a reason why it could change in the future, there is no guarantee that this won't happen.
I am trying to figure out how to access two (or more) parameters passed to a Laravel controller. I know how to create the route, and the URL is created correctly, but then I can only access the first passed parameter in my controller.
Route:
Route::get('managers/{id}/{parameter2}', array('as'=>'dosomething', 'uses'=> 'ManagersController#dosomething'));
where the first parameter is obviously the $id for managers, and the second parameters is to be processed by the controller.
View:
Do Something
generates the URL:
http://domain/managers/1/2
where 1 is easily accessed as the $id for managers, but when I try to access the 2nd parameter "2" using $parameter2, e.g. using a simple return: "id=$id and parameter2=$parameter2" statement, I get an "unidentified variable: $parameter2" error.
What am I doing wrong?
Is there a better way to pass multiple parameters? I'm especially asking the "better way?" question because what I want to do is use the 2nd parameter to change a value in a database table, and using a 'get' method, somebody could change the parameter value in the URL and therefore cause mischief. Must I use a 'post' method? I'd love to be able to use a link, since that works much better with the design of my application.
Thanks!
I was asked to include the controller, which I'm happy to do. Initially, just for testing, as I mentioned, my controller was a simple return to display the values of the two passed parameters. But here is what I want to be able to do, including the actual name of the function ("update_group" rather than "dosomething") --
ManagersController:
public function update_group($id)
{
DB::table('groups')->where('id','=',$parameter2)->update(array('manager_id'=>$id));
return Redirect::route('managers.show', array('id'=>$id));
}
The update table works perfectly if I replace $parameter2 with an actual value, so that syntax is fine. The issue is that Laravel says that $parameter2 is an undefined variable, despite the fact that the URL contains the value of $parameter2 as you can see above.
And since it occurs to me that the answer to this may involve adding a function to the Manager model, here is the current
Manager.php
class Manager extends Eloquent {
protected $table = 'managers'; ... (mutator and error functions)
}
Just change
public function update_group($id)
to
public function update_group($id, $parameter2)
All looks ok in your route. Seeing the controller code would help, but likely, you may not have a second parameter in your controller's dosomething() method.
public function dosomething($id, $parameter2){
var_dump($id).'<br />';
var_dump($paremter2);
}
If that isn't the case, you can try dumping it from the route's callback to further diagnose.
Route::get('managers/{id}/{parameter2}', function($id, $parameter2)
{
var_dump($id).'<br />';
var_dump($paremter2);
});
Depending on your use case, you can pass them in a query string like so: but it isn't really the 'best way', unless you're doing something like building an API that won't use the same variables in the same order all the time.
/managers?id=1¶mter2=secondParameter
var_dump(Request::query('id')).'<br />';
var_dump(Request::query('paramter2'));
Is there any way I can have a route set up with one regex section that doesn't get passed as a parameter?
For example:
Route::get('{string}/method/{id}', function($id)
{
return 'only one parameter passed, ID is ' . $id;
});
Specifically I'm routing to a controller and the methods need to be compatible with routes coming from elsewhere, which don't include this first parameter.
The most important thing is that, routes has to be matched according to it's declaration, for example, if you define a route like your example here
Route::get('{string}/method/{id}', function($id)
{
return 'only one parameter passed, ID is ' . $id;
});
Then the requested url has to be matched with same numbers of parameters including the http method (GET here) and in this case, the route will match only with something like this
httP//example.com/something/method/10
Here the second parameter 10 is not bound to be digits because you didn't make it to be a digit using where(...) so, it could be anything but two parameters must be required.
As an alternative, you may define a missing method in your controller like this (An idea only)
public function missingMethod($args = array())
{
// $args will contain all parameters
}
This is a special method in Laravel that any controller may contain it and whenever a non-existing method will be called in that controller, then this missingMethod would be called and all the parameters will be passed to it's $args parameter as an array, so if you have two parameters in the url and calling method is missing in the controller then you may get those parameters within this method scope, something like [param1, param2] and from this method you may call your desired method depending on the count of params.
So, if you just point the route's action to this controller which has the missing method then from the missingMethod you may call another method using different parameters according to your other method.
Also check PHP Overloading and call_user_func_array to get the real idea of missingMethod.