Laravel get unamed "GET" parameter id from URL - php

I am trying to get a value from the url that looks more or less like this:
http://localhost:8000/new-user/7
This number 7 passed in the url as a parameters is an ID in which i submit from the blade form as a request for an action i perform in the controller but i cant get this value in anyway.
This is what i tried so far:
I tried to use this in the controller in which i submitted the form
$request->route('company_id');
I also tried to get this as a proper GET parameter:
<input type="hidden" name="company_id" id="company_id" value="{{app('request')->input('company_id')}}">
and i also tried this:
<input type="hidden" name="company_id" id="company_id" value="{{Input::get('company_id')}}">
and this:
<input type="hidden" name="company_id" id="company_id" value="{{$_GET['company_id']}}">
None os these options work and i still receive an empty value.
Any ideas or suggestions on how can i get this variable?
Thank you!

A route parameter and a query parameter are two different things.
If you have a route defined like this:
/** routes/web.php */
Route::get('/new-user/{id}', 'UsersController#show');
In this case $id is a route parameter. So to get in your blade view you could do:
/** resources/my_view.blade.php */
{{ request()->id }}
So, with a request like the one you used http://localhost:8000/new-user/7 that should output: 7.
Another case is when you have a query param. These variables doens't need to be defined in the route. For example a call of this type, using the same route defined in the previous example:
GET http://localhost:8000/new-user/7?foo=bar
^^^^^^^^
In this case the foo=bar can be accessed like this:
{{ request()->query('foo') }} // 'bar'

Related

how to pass html checkbox arrays to controller in route laravel?

I have a problem with Laravel framework .... I Have an HTML array of checkboxes in my Laravel view file like below:
#foreach($jobs as $job)
<input type="checkbox" name="jobs[]" value="{{$user->id}}">
#endforeach
//$jobs variable pointing to read records from jobs table in database
I want to pass "jobs" element to my Controller from the route file But I don't want to use jQuery to do it. (Should be passed with Get Method].
How can I do it?
<form method="get" action="{{route('some.route')}}">
#foreach($jobs as $job) <input type="checkbox" name="jobs[]" value="{{$user->id}}">
#endforeach
<input type="submit" value="submit">
</form>
web.php
Route::get('some/route','yourcontroller#somemethod')->name('some.route')
yourcontroller.php
public function somemethod(Request $request){
dd($request->get('jobs'));
//you an store it in database now
}
As far as I can understand your question, in order to pass data from your input to the controller you need to have a <form> that contains it, and set your method attribute to GET. From there, you can make a Route that will accept a parameter.
Read the Routing docs here.

Passing the language parameter to the next page (url) when submitting the form

I'm having problems with getting the right language parameter in the url when submitting a form.
My application has two possible language parameters: de and en.
I've made a middleware called setLocale.php with this function:
public function handle($request, Closure $next)
{
$locale = $request->segment(1);
app()->setLocale($locale);
return $next($request);
}
this is the route in web.php:
Route::post('/{locale?}/ticket', 'TicketController#store')->name('validation');
and here is the form action:
<form method="post" action="{{ route('validation') }}">
When I want to submit the form and the input is validated you should get to the route I've shown you above.
But the url will only be: /ticket instead of en/ticket or de/ticket.
By the way, you get to the form site via links, so the language parameters are static like this:
Route::get('/en/index/{param1?}/{param2?}', 'TicketController#index')->name('index');
Route::get('/de/index/{param1?}/{param2?}', 'TicketController#index')->name('index');
How do I get the language parameter from the form page ('/en/index/[..]') in the url of the validation page, therefore in the form action?
Shouldn't the handle function in the setLocale.php middleware get me the language parameter when trying to submit the form?
If yes, how can I achieve passing it to the next page (via the form action)?
EDIT
It works fine when I edit the form action like this:
<form method="post" action="{{ route('validation', app()->getLocale()) }}">
Would this be a good solution?
As I understand, your problem is that the {locale} parameter has no value if you don't explicitly pass one to the route() function. You can use route binding (see "Customizing The Resolution Logic") to guarantee that it will always have a value, falling back on the current locale:
Route::bind('locale', function ($value) {
if (!$value) {
$value = app()->getLocale();
}
return $value;
});
This instructs Laravel to call this function whenever it sees a route parameter named locale and use its return value. And if there's no value for the parameter, it returns the current locale.
But be aware that this will affect all URL parameters named locale. If you have any route whose locale parameter should be allowed to be empty, you will have to rename in in one of the routes.

Laravel : Missing required parameters for route

I have the following route :
Route::post('new-password/{id}/{random}',['as'=>'postNewPassword','uses'=>'AbcController#postNewPassword']);
And I am using that route in form action and passing the required parameters like:
action="{{ route('postNewPassword',Request::segment(2),Request::segment(3))}}"
The Url is :
http://localhost:8080/new-password/14/yxbH1sP4mdRngtCqQ9VS1KeksadOf5Piwc784HeQ
I don't know what am I missing. What am I doing wrong here?
You need to pass all route paramters as the seconds parameter, so change code to:
route('postNewPassword', [Request::segment(2), Request::segment(3)])

Forms in Symfony2

I'm trying to create a simple search form in Symfony2.
This is my form:
<form action="/search" method="GET">
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search ...">
<span class="input-group-btn">
<button class="btn-u btn-u-lg" type="button"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
So my question is, what is the syntax of the form action? Do I just enter in the exact file that receives it? Or do I need to call some kind of config file?
Another question is how do I handle the search in the controller?
Thanks in advance!
Please note that I'm a total noob in Symfony2 :)
EDIT:
How do I handle the request if I would like a nice url like this: ".../search/value" instead of ".../search?q=value"?
Here is my action:
/**
* #Route("/search/{value}", name="search")
*/
public function searchAction($value)
{
}
in your action you need to put the logical path of your controller to do so call the twig function {{path('you route alias')}} , I assume that you have already set your route configuration.
to handle search in controller that's will depend on you re own logic but in the general case you will have to get the searched word using the request object taht should be some think like that:
public function searchAction(Request $request){
$objet=$request->query->get("word");
//do staff
return $this->render('Your Bundle:views:searchResult.html.twig')
}
In the form action, you need to enter the route that points to your controller. For example if you have a route that is set up to point to "/search" which uses your searchController's search method, you'd have to write action="/search" so when a user submits this form it is going to point to your controller's method.
In your controller you can either grab the $_GET variable with the name of the input (by the way you need to add a name for your input field to be accessible via the request superglobals), or pass the value directly in your url and put an optional variable after your "/search".

Laravel multiple route aliases

I'm trying to create a route with an array of aliases, so when I call whois or who_is in the url it goes to the same route.
Then I don't need to keep repeating the code every time, changing only the alias.
I tried the code below.
Variables in the routes:
$path = 'App\Modules\Content\Controllers\ContentController#';
$aliases['whois'] = '(quemsomos|who_is|whois)';
Routes:
Route::get('{whois}', array('as' =>'whois', 'uses' => $path.'getWhois'))->where('whois', $aliases['whois']);
this one works as well
Route::get('{whois}', $path.'getWhois')->where('whois', $aliases['whois']);
Typing in the url my_laravel.com/whois or my_laravel.com/who_is or my_laravel.com/quemsomos will send me to $path.'getWhois' (which is correct).
But when I try to call it in the html on blade...
Who we are
The reference link goes to my_laravel.com//%7Bwhois%7D
How could I call route('whois') on my blade.php and make it work like when I type it on the url ?
I would like to use the route function` in my blade, so I can keep a pattern.
During route generation using the route function, Laravel expects you to set the value of the route parameter. You are leaving the parameter whois empty so the parameter capturing {whois} will not be replaced and results in the %7B and &7D for the accolades.
So in order to generate a route you will need to define what value you'd like to use for whois; {{ route('whois', ['whois'=>'whois']) }} for instance.

Categories