how to extract a specified url parameter in laravel - php

Hello I have a specified URL https://www.example.com/doctor
I want to extract the parameter doctor from the URL so that I can compare that the URL has a doctor in it or not.

You should look at Laravel's Routing docs: https://laravel.com/docs/5.5/routing
Something like this to retrieve "doctor" out of your example url:
Route::get('{$item}', function ($item) {
return $item; // https://www.example.com/doctor => "doctor"
});

Related

How to get a segment URL cakephp 3

Hi I got trouble in retrieve URL segment CAkephp3 in view. I want to get the ID from current URL.
Lets say my URL is http://localhost/admin/financial_agreements/edit/50
and I want redirect to http://localhost/admin/financial_agreements/do_print/50
simply :
var urlPrint = "<?=$this->Url->build(['controller' => 'financial_agreements', 'action' => 'do_print', 'I NEED ID FROM CURRENT'], true)?>";
I try debug
<?=debug($this->Url->build()); die();?>
But its produce : admin/financial_agreements/edit/50
whats called in 50 ? I need that 50 inside my "url->build" urlPrint
sorry for bad english.
Anyhelp will appreciate.
thanks.
You can use the Request object to get request data (including url parameters) within views.
Try this in your view:
$this->request->getParam('pass') //CakePHP 3.4+
$this->request->params['pass'] // CakePHP 3.3
That will return an array of all non-named parameters that were passed after the action's name in the URL. Example: /mycontroller/myaction/param1/param2. So in this example, $this->request->getParam('pass') will produce an array like: [0 => 'param1', 1 => 'param2'].
Bonus answer: you can also 'name' parameters in the URL, like: /mycontroller/myaction/some_name:some_value. To retrieve this kind of named parameters, you would do the same trick but using: $this->request->getParam('named') (Use the argument 'named' instead of 'pass').
More info:
https://book.cakephp.org/3.0/en/controllers/request-response.html
https://book.cakephp.org/3.0/en/development/routing.html#passed-arguments
Assuming that your edit function follows standard practices, you'll have something like this:
public function edit($id) {
$financialAgreement = $this->FinancialAgreements->get($id);
...
$this->set(compact('financialAgreement'));
}
Then in edit.ctp, you can get the id of the current record very simply as $financialAgreement->id, so your URL will be generated with
$this->Url->build(['controller' => 'financial_agreements', 'action' => 'do_print', $financialAgreement->id], true)

jQuery pass a input field value to a get method redirect url laravel 5

I'm working a Order List using Laravel 5 and I have this Reject button which is like this
When it is clicked it will confirm if the user really want to reject then if yes it will redirected to a specified route like so
Route::get('reject-order/{ordernum}', 'OrderController#rejectCustomerOrder');
Then in my rejectCustomerOrder
public function rejectCustomerOrder($ordernum)
{
var_dump(Input::get('reject_reason')); exit;
CustomerOrder::where('order_number', '=', $ordernum)->update(['status' => 2]);
$data = CustomerOrder::where('order_number', '=', $ordernum)->get();
$user = User::find($data[0]->created_by_id);
Mail::send('emails.message-rejected', ['user' => $user->name, 'order_num' => $ordernum], function ($m) use ($user) {
$m->to($user->email, '')->subject('Custtomer Order Rejected');
});
Problem is i can't get the reject_reason input field. It's always null. When you click the x button (reject button) it will ask if you really want to reject and you need to put the reject reason on the text field. How can I get that or pass it in the route?
Try to pass the following parameter in this method.
rejectCustomerOrder($ordernum,Request $request)
the access this way:
$request->reject_reason
Fixed now. :) Was able to so by making it
<a class="btn btn-danger btn-ok" id="btnReject">Reject</a>
And adding
$("#reject_reason").keyup(function() {
var oldDataHref = $("#btnReject").attr('href');
var newDataHref = oldDataHref + $('#reject_reason').val();
$("#btnReject").attr('href', newDataHref);
});
If it's a "post" route you don't need the parameter in route definition. If it's a "any" or "get" type then it's ok to have it there.
So, if it's POST then you can get the value with Input::get('input_name') and if it's get you can simply get it trought the controller method parameter, in your case $ordernum.

right way to construct and accept GET request in Laravel with long parameter (array)

I am new to Laravel. I do not know the right way to construct and accept GET requests.
I need to send the following request (en and es are language codes):
translation/next-word/en/es
and in Controller I have
public function getNextWord($langfrom, $langto) {
However, now new requirement came and I also have to send a list of IDs (on my client side it is an array (for instance, [1,5,12,15]), but it could be long list (about 100 IDs). Thus I am not sure how to send this ID array to controller and also accept it in controller method.
My old client side request (without categories):
// var categories = [1,2,5,6,17,20];
var url = "translation/next-word/en/es";
$.ajax({
url: url,
method: "GET"
}).success(function(data){
...
});
For get, change your controller like this,
public function getNextWord() {
$langfrom = $_GET['langfrom'];
$langto = $_GET['langto'];
}
In ajax send the data like this,
$.ajax({
url: url,
method: "GET" ,
data: {langfrom:langfrom,langto:langto} <----- passing from GET
}).success(function(data){
...
});
If you wan to get in parameters like this,
public function getNextWord($langfrom, $langto) {
Then ajax should look like this,
$.ajax({
url: url +"/" + langfrom + "/" langto, <----- passing as parameter
method: "GET" ,
}).success(function(data){
...
});
In Laravel, you handle GET requests by making a route in your routes.php file and then a corresponding method in a controller class to actually handle the request. In your case, because you want to also pass in an unknown number of IDs, I would suggest making that into a query parameter. You can format that however you want, although I would suggest using something like commas to divide the data in your URL. So in the end, your URL would look something like this:
example.com/translation/next-word/en/es?categories=1,2,5,6,17,20
routes
Route::get('translation/{nextWord}/{from}/{to}', 'TranslationController#translate');
TranslationController
public method translate($nextWord, $from, $to, Request $request)
{
//get the categories passed in as a query parameter
$input = $request->all();
$categories = $input['categories'];
$categories = explode(',',$categories); //turn string into array
//actually translate the words here (or whatever you need to do)
$translated = magicTranslateFunction($nextWord, $from, $to);
//also you can now use the categories however you need to
//once you're done doing everything return data
return $translated;
}
Inside your javascript, you'll just want to turn your array of categories into a comma delimited string and pass that to make the URL I started the post with.
var categories = [1,2,5,6,17,20];
var categoriesString = categories.join();
var url = "translation/next-word/en/es?categories="+categoriesString;
$.ajax({
url: url,
method: "GET",
success: function(data) {
...
}
});
Edit - using $.ajax 'data' setting
Instead of appending the categories as a string to the URL, you can just pass in the array directly as part of the 'data' setting of your ajax call.
var categories = [1,2,5,6,17,20];
var url = "translation/next-word/en/es";
$.ajax({
url: url,
method: "GET",
data: {
"categories": categories
},
success: function(data) {
...
}
});
Laravel will actually convert this correctly to a PHP array, so you don't need to do any special parsing in your controller. Just take it in like normal and use it:
TranslationController
public method translate($nextWord, $from, $to, Request $request)
{
//get the categories passed in as a query parameter
$input = $request->all();
$categories = $input['categories']; //already a PHP array
//actually translate the words here (or whatever you need to do)
$translated = magicTranslateFunction($nextWord, $from, $to);
//also you can now use the categories however you need to
//once you're done doing everything return data
return $translated;
}

get parameters from url in laravel

I have some filters on my view and I want to get the parameters of my current URL and do something like edit any of my items in the page and go back with all the filters again after edit.
My example URL:
localhost:8000/equipamentos/filtro?filter_descricao=APARELHO+ULTRASSOM&filter_patrimonio=0
Then I choose any item to edit and go to:
localhost:8000/equipamentos/332/edit
After I change something I want to be redirected to the same URL with the filters in the beginning, like redirect and append filtro?filter_descricao=APARELHO+ULTRASSOM&filter_patrimonio=0
Thanks!
Use the Input facade:
// All
$data = Input::all();
// $_REQUEST['foo']
$data = Input::get('foo'); // null if foo doesn't exist
$data = Input::get('foo', 'bar'); // if foo doesn't exist, the value is bar
Then you can handle the redirection in the controller on in a filter.
I just drafted out the code and it is working.
Route::get('/query', function() {
return Redirect::route('result', Input::query());
});
Route::get('/result', [ 'as' => 'result', 'uses' => function() {
return Response::make(Input::all());
}]);
Either Input::all() or Input::query() should work to retrieve GET parameters.
I'm using Laravel 4.2.11

Yii pattern match for URL

I need to handle a SEO-friendly URL in my Yii application. My URL structures in the pattern: "domain.com/url-string/uniqueid". For example:
domain.com/properties-in-dubai/325fgd
domain.com/properties-in-USA/4577ds6
domain.com/properties-in-india/567dew
domain.com/apartments-in-anderi-mumbai/645fki
The above URL strings and ID are populated by us. When the user access this URL:
first need to validate the URL pattern.
second extract the I'd and URL string and match with my data store.
third when above URL and I'd exist in our data store pass a referenced parameters to existing search page.
Kindly, anyone help me solve this issue and give sleep to me.
First, you add a new rule to your urlManager application configuration.
'rules' => array(
'<slug>/<id:\d+>' => 'property/view'
// ...
)
Then you can retrieve the slug and ID in the action:
class PropertyController extends CController
{
public function actionView($slug, $id)
{
$id = (int) $id;
// Check if $id, $slug exist; replace line below
$exists = true;
if($exists){
// Redirect to elsewhere
$this->redirect();
}
}
}

Categories