How to generate url with params in Laravel? - php

The situation is quite tricky... For example I have URL
http://example.com/users
which shows all users. I also have few filters like
http://example.com/users?sort_type=ASC&sort_value=surname
or
http://example.com/users?sort_type=DESC&sort_value=name.
There also is the search which looks like this
http://example.com/users/search/by_name/?search_value=bob
or http://example.com/users/search/by_surname/?search_value=miller.
For the search I also need to add filtering params, so the main problem is in first symbol: when there is the list of users it should be ?, when search &. So is there some url generation function for generating url from URL params?

All values not using by route mask will append as query params. (change route name from 'users' to yours)
URL::route('users', array(
'sort_type' => 'ASC',
'sort_value' => 'surname'
));
If you don't use routes then use something like this
$query = http_build_query(array(
'sort_type' => 'ASC',
'sort_value' => 'surname'
));
URL::to(action('UserController#index') . '?' . $query );

Related

How to add multiple 'Get' on URL?

Im working on a php project and first a have a value on url for example :
http:www.c/com/app.php?doctor_id=12
And then i want to add an another value on that url without losing the doctor id
For example : http:www.c/com/app.php?doctor_id=12?appoin_date=11pm
Also when i get the doctor id it redirects the appoin page :)
Do not generate a URL-encoded query string yourself. Use the http_build_query function for this. The function also takes over the necessary escaping of the parameters. Example:
$url = 'http:www.example.com/app.php';
$parameters = [
'doctor_id' => 12,
'appoin_date' => '11pm',
'name' => 'max&moritz'
];
$url .= '?'.http_build_query($parameters);
//"http:www.example.com/app.php?doctor_id=12&appoin_date=11pm&name=max%26moritz"

How to pass url(include '&' operator) in query string without encode at direct address bar?

I'm used laravel framework.
How to pass url(include '&' operator) in query string without encode at write direct address bar?
Like this: (without encode)
https://test.com/test?q=test&url=http://ddd.com?dd=dd&aa=aa&cc=cc
I want below like result query params in code
['q' => test, 'url' => 'http://ddd.com?dd=dd&aa=aa&cc=cc']
How can do i?
Since Laravel will try and remove http:// from the string, you can try adding the url like this.
url('/my-url', [
'q' => 'test',
]) . 'url?=http://..../';
// or
route('my.route.get', [
'q' => 'test',
]) . 'url?=http://..../';
Say your route looks like this:
Route::get('/{id}, array('as' => 'test.route', function($id){
return $id;
}));
You would generate a link like this in your view
click here
Your url would look like this:
http://localhost:8000/1?something=here&another=there
more

Laravel 5.1 add Query strings in url

I've declared this route:
Route::get('category/{id}{query}{sortOrder}',['as'=>'sorting','uses'=>'CategoryController#searchByField'])->where(['id'=>'[0-9]+','query'=>'price|recent','sortOrder'=>'asc|desc']);
I want to get this in url: http://category/1?field=recent&order=desc
How to achieve this?
if you have other parameters in url you can use;
request()->fullUrlWithQuery(["sort"=>"desc"])
Query strings shouldn't be defined in your route as the query string isn't part of the URI.
To access the query string you should use the request object. $request->query() will return an array of all query parameters. You may also use it as such to return a single query param $request->query('key')
class MyController extends Controller
{
public function getAction(\Illuminate\Http\Request $request)
{
dd($request->query());
}
}
You route would then be as such
Route::get('/category/{id}');
Edit for comments:
To generate a URL you may still use the URL generator within Laravel, just supply an array of the query params you wish to be generated with the URL.
url('route', ['query' => 'recent', 'order' => 'desc']);
Route::get('category/{id}/{query}/{sortOrder}', [
'as' => 'sorting',
'uses' => 'CategoryController#searchByField'
])->where([
'id' => '[0-9]+',
'query' => 'price|recent',
'sortOrder' => 'asc|desc'
]);
And your url should looks like this: http://category/1/recent/asc. Also you need a proper .htaccess file in public directory. Without .htaccess file, your url should be look like http://category/?q=1/recent/asc. But I'm not sure about $_GET parameter (?q=).

CakePHP 3.0 query string parameters vs passed parameters

In CakePHP 3.0 named parameters have been removed (thank god) in favour of standard query string parameters inline with other application frameworks.
What I'm still struggling to get my head around though is that in other MVC frameworks, for example ASP.NET you would pass the parameters in the ActionResult (same as function):
Edit( int id = null ) {
// do stuff with id
}
And that method would be passed the id as a query string like: /Edit?id=1 and you'd use Routing to make it pretty like: /Edit/1.
In CakePHP however anything passed inside the function parameters like:
function edit( $id = null ) {
// do stuff with $id
}
Must be done as a passed parameter like: /Edit/1 which bypasses the query string idea and also the need for routing to improve the URL.
If I name the params in the link for that edit like:
$this->Html->link('Edit', array('action' => 'edit', 'id' => $post->id));
I then have to do:
public function edit() {
$id = $this->request->query('id');
// do stuff with $id
}
To get at the parameter id passed. Would of thought it would pick it up in the function like in ASP.NET for CakePHP 3.0 but it doesn't.
I prefer to prefix the passed values in the edit link instead of just passing them so I don't have to worry about the ordinal as much on the other end and I know what they are etc.
Has anyone played with either of these ways of passing data to their methods in CakePHP and can shed more light on the correct ways of doing things and how the changes in version 3.0 will improve things in this area...
There are a few types of request params in CakePHP 3.0. Let's review them:
The Query String: are accessed with $this->request->query(), are not passed to controller functions as arguments and in order to make a link you need to do Html->link('My link', ['my_query_param' => $value])
Passed arguments: The special type of argument is the one that is received by the controller function as an argument. They are accessed either as the argument or by inspecting $this->request->params['pass']. You Build links with passed args depending on the route, but for the default route you just add positional params to the link like Html->link('My link', ['action' => view, $id, $secondPassedArg, $thirdPassedArg])
Request Params: Passed arguments are a subtype of this one. A request param is a value that can live in the request out of the information that could be extracted from the route. Params can be converted to other types of params during their lifetime.
Consider this route:
Router::connect('/articles/:year/:month/:day', [
'controller' => 'articles', 'action' => 'archive'
]);
We have effectively created 3 request params with that route: year, month and day and they can be accessed with $this->request->year $this->request->month and $this->request->day. In order to build a link for this we do:
$this->Html->link(
'My Link',
['action' => 'archive', 'year' => $y, 'month' => $m, 'day' => $d]
);
Note that as the route specify those parameters, they are not converted as query string params. Now if we wanted to convert those to passed arguments, we connect this route instead:
Router::connect('/articles/:year/:month/:day',
['controller' => 'articles', 'action' => 'archive'],
['pass' => ['year', 'month', 'day']]
);
Our controller function will now look like:
function archive($year, $month, $day) {
...
}

Yii: How to redirect and go to a specific element on page?

I am trying to redirect and go to a specific element on the new page like this:
http://192.168.0.49/x/y/index.php/admin/user/update/id/3#certificate
$this->redirect(array('update', 'id' => $certificate->user_id));
How can this be done?
You can simply create the url without the fragment part and then append it manually:
$url = Yii::app()->createUrl('update', ['id' => $certificate->user_id]);
$url .= "#certificate";
$this->redirect($url);
This code works in a manner that is immediately obvious when reading the code. Apart from that there is also the Yii-specific solution: CUrlManager (the component responsible for building URLs) also recognizes # as a parameter. So you can write:
$url = Yii::app()->createUrl(
'update',
['id' => $certificate->user_id, '#' => 'certificate']
);
That can't be done using redirect.
A work around would be
$url = Yii::app()->createUrl('update', array('id' => $certificate->user_id, '#' => "certificate"));
$this->redirect($url);

Categories