Questionmark in url - laravel

I'm trying to use this http://api.jqueryui.com/autocomplete/#option-source with Laravel and so I need to send a GET request to a url which ends with "?term=foo". I've tried to escape the "?" with a backslash, which doesn't work. To clarify, this is what I want:
Route::get('search\?term=(:any)', function()
{
//do something
}
Is it possible to have questionmarks in the url with Laravel?

Just for others who may want a Clear Answer :
you have to write and use your code as follows:
Route::get('search', function()
{
$term = Input::get('term');
if(isset($term)){
//do other stuff !
}
}

Having a question mark in the URL should make no difference. You're using a PHP framework, and simply speaking, ...?term=parameters should not be problematic. To my knowledge, there should be no need to escape such a question mark... It is handled appropriately by default.

I believe the slug function is what you are looking for: http://laravel.com/api/class-Laravel.Str.html
From the API Doc:
slug( string $title, string $separator = '-' )
Generate a URL friendly "slug" from a given string.

Related

How to include a variable in a laravel redirect

I have a variable in a Laravel 5 controller which I am trying to include in the url of a redirect link, however nothing I've tried seems to work, my code is currently as follows.
$newChallenge = ((int) request('challenge_id') + 2);
return redirect('/texttext/{{$newChallenge}}');
However, this doesn't use the variable represented by $newChallenge, but rather
"{{newChallenge}}" as a string, how should this be done?
{{ }} is meant for use in blade files.
I think you can do return redirect('/texttext/{ $newChallenge }'); if you really want those { } brackets.
Or just return redirect(sprintf('/texttext/%s'), $newChallenge));
or return redirect('/texttext/'.$newChallenge);
Edit: Also, your redirect could probably benefit from being a named route. https://laravel.com/docs/5.8/routing#named-routes
May be you can change it to a query parameter
like
return redirect('/texttext/?=newChallenge={{$newChallenge}}');
if you want to use
return redirect('/texttext/{{$newChallenge}}');
It should be something like below
Route::get('texttext/{{newChallenge}}', 'YourController#newChallenge');
and newChallenge signature will function newChallenge(Request $request, $newChallenge)
if you want to use something like return redirect(route('new-challenge', $newChallenge));
just add name to route like
Route::get('texttext/{{newChallenge}}', 'YourController#newChallenge')->name('new-challenge');
I have not tested, but I think it should work.

Laravel passing all routes for a particular domain to a controller

Working on a Laravel 4.2 project. What I am trying to accomplish is pass every URI pattern to a controller that I can then go to the database and see if I need to redirect this URL (I know I can do this simple in PHP and do not need to go through Laravel, but just trying to use this as a learning experience.)
So what I have at the moment is this:
Route::group(array('domain' => 'sub.domain.com'), function()
{
Route::get('?', 'RedirectController#index');
});
I am routing any subdomain which I deem as a "redirect subdomain" ... The ? is where I am having the problem. From what I have read you should be able to use "*" for anything but that does not seem to be working. Anyone have a clue how to pass any URL to a controller?
And on top of that I would ideally like to pass the FULL URL so i can easily just check the DB and redirect so:
$url = URL::full();
Try this:
Route::group(array('domain' => 'sub.domain.com'), function()
{
Route::get('{path}', 'RedirectController#index')
->where('path', '.*');
});
And your controller will reseive the path as first argument
public function index($path){
// ...
}
In case you're wondering, the where is needed because without it {path} will only match the path until the first /. This way all characters, even /, are allowed as route parameter

Laravel how to route old urls

I am using Laravel 4.
I have an old url that needs to be routable. It doesn't really matter what it's purpose is but it exists within the paypal systems and will be called regularly but cannot be changed (which is ridiculous I know).
I realise that this isn't the format url's are supposed to take in Laravel, but this is the url that will be called and I need to find a way to route it:
http://domain.com/forum/index.php?app=subscriptions&r_f_g=xxx-paypal
(xxx will be different on every request)
I can't figure out how to route this with laravel, i'd like to route it to the method PaypalController#ipbIpn so i've tried something like this:
Route::post('forum/index.php?app=subscriptions&r_f_g={id}-paypal', 'PaypalController#ipbIpn');
But this doesn't work, infact I can't even get this to work:
Route::post('forum/index.php', 'PaypalController#ipbIpn');
But this will:
Route::post('forum/index', 'PaypalController#ipbIpn');
So the question is how can I route the url, as it is at the top of this question, using Laravel?
For completeness I should say that this will always be a post not a get, but that shouldn't really make any difference to the solution.
Use this:
Route::post('forum/{file}', 'PaypalController#ipbIpn');
And then in the controller, use
public function forum($file) {
$request = Route::getRequest();
$q = (array) $request->query; // GET
$parameters = array();
foreach($q as $key => $pararr) {
$parameters = array_merge($parameters, $pararr);
}
}
You can then access the get parameters via e.g.
echo $parameters['app'];
you can use route redirection to mask and ending .php route ex:
Route::get('forum/index', ['uses'=> 'PaypalController#ipbIpn']);
Route::redirect('forum/index.php', 'forum/index');

%20 is added instead of spaces

I guess this is small issue but yet i had to ask here since i am running short in my project. When I pass string to the function in another controller, it changes spaces into %20 sign. I guess the controller thinks the string passed as url and encodes it. But I don't know exactly how to remove it or if possible do not let it to change spaces into %20. Here is the code which i use;
$message="The user name you provided is already in our database";
redirect('admin/add_user/'.$message);
Here is my controller function where i receive the message;
public function add_user($message)
{
echo $message;
}
I also tried this as;
public function add_user()
{
echo $this->uri->segment(3);
}
But the result is same. Here is the output of the string;
The%20user%20name%20you%20provided%20is%20already%20in%20our%20database
Try this:
public function add_user($message)
{
echo urldecode($message);
}
You can read more about urldecode here: http://php.net/manual/en/function.urldecode.php
Try this:
echo urldecode($message);
because you are passing the message as part of the URL (The redirect does a new http request) it is automatically url encoded. You just need to decode it once the server receives it.
When saving to the database use:
htmlentities($variable)
when outputing use:
echo html_entity_decode($variable, ENT_COMPAT, 'UTF-8');

CodeIgniter: urlencoded URL in URI segment does not work

I'm trying to put a URL as the value of one of my URI segments in CI. My controller method is defined to accept such an argument. However, when I go to the URL, I get a 404 error. For example:
www.domain.com/foo/urlencoded-url/
Any ideas what's wrong? Should I do this via GET instead?
UPDATE:
// URL that generates 404
http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F
// This is in my profile_manager controller
public function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '', $url_current = '')
If I remove the second URI segement, I don't get a 404: http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338/
It seems that the %2F breaks things for apache.
Possible solutions:
preg_replace the /'s to -'s (or something else) before sending the url then switch it back on the other end.
Set apache to AllowEncodedSlashes On
bit of a hack, but you could even save the url to a session variable or something instead of sending through the url *shrug *
double url encode it before sending
Pass urlendode()'d URL in segment and then decode it with own (MY_*) class:
application/core/MY_URI.php:
class MY_URI extends CI_URI {
function _filter_uri($str)
{
return rawurldecode(parent::_filter_uri($str));
}
}
// EOF
You may need to change the rule in config/route.php to accept the encoded characters in URL. Also you can take a look at some of the solution from below articles:
http://codeigniter.com/forums/viewthread/81365/
http://sholsinger.com/archive/2009/04/passing-email-addresses-in-urls-with-codeigniter/
Passing URL in Codeigniter URL segment
I actually had to do urlencode(urlencode(urlencode(
and urldecode(urldecode(urldecode(
3 times!! and it finally worked, twice didn't cut it.
try
function __autoload($class){
if(!empty($_SERVER['REQUEST_URI'])){
$_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_QUERY_STRING'] = $_SERVER['QUERY_STRING'] = $_SERVER['REDIRECT_URL'] = $_SERVER['argv'][0] = urldecode($_SERVER['REQUEST_URI']);
}
}
in config.php
this method work for me
This is very old, but I thought I'd share my solution.
Instead of accepting the parameter as a url path, accept it as a get variable:
http://localhost/myapp/profile_manager/confirm_profile_parent_delete/ugpp_533333338?url_current=http%3A%2F%2Flocalhost%2Fmyapp%2Fdashboard%2F
and in code:
function confirm_profile_parent_delete($encrypted_user_group_profile_parent_id = '') {
$url_current = $this->input->get('url_current');
...
This seems to work.

Resources