Is there any reason why a urlencoded string would throw a 404 error? - urlencode

I'm trying to pass two parameters, one of which is an email address.
routes (also tried (:any))
Route::any(
'user/confirm_request/(:any?)/(:any?)', array(
'uses' => 'user#confirm_request'));
controller (also tried post_confirm_request())
public function get_confirm_request($email, $term)
{
//do stuff
}
Ultimately, all I'm trying to do is hit that route and send an email to a user with those two parameters. But I keep getting a 404 error. The email gets encoded and the route looks like this:
/email%40gmail.com/someString
I'm able to take out %40 and it works just fine (just gives me an error for the sendmail).
Why would the %40 be throwing a 404 error? Could it be a Laravel thing?

One solution would be to pass the email as a url parameter.
First, remove the second argument from the route. (You could also remove both if needed.)
Route::any('user/confirm_request/(:any?)', array('uses' => 'user#confirm_request'));
Then append the email to the action url, something like this..
$url = URL::base() . '/user/confirm_request?email=' . $email;
Then in your controller, you can grab that data.
public function get_confirm_request()
{
$email = Input::get('email');
}

Related

Yii2 remove form name from url parameters

I use Yii2 forms and after submit my URL looks like:
http://example.com/recommendations/index?RecommendationSearch%5Bname%5D=&RecommendationSearch%5Bstatus_id%5D=&RecommendationSearch%5Btype%5D=0&RecommendationSearch%5Bcreated_at%5D=&sort=created_at
As you may seem each parameter contains form name RecommendationSearch. How to remove this RecommendationSearch from parameters in order to get URL url like the following:
http://example.com/recommendations/?name=&status_id=&type=0&created_at=&sort=created_at
You need to override formName() in your RecommendationSearch model to return empty string:
public function formName() {
return '';
}

Phalcon router sends the wrong parameters

I have a problem with my router in Phalcon.
I have an action in my controller which ether takes a date parameter or not.
So when I access an URL: http://example.com/sl/slots/index/2017-06-27
everything works ok.
But when I go to: http://example.com/sl/slots/index
I get the following error:
DateTime::__construct(): Failed to parse time string (sl) at position
0 (s): The timezone could not be found in the database.
So the router actually takes the "sl" in the beginning as a parameter.
My router for this kind of url is set like this:
$router->add(
"/{language:[a-z]{2}}/:controller/:action",
array(
"controller" => 2,
"action" => 3
)
);
Btw it does the same withut the index: http://example.com/sl/slots
Oh and my slots index action looks like this:
public function indexAction($currentDate = false){ //code }
So the $currentDate is set to "sl" when I call the action without a parameter
Thank you for the help
Well you need to add language in first argument of action too. Then it should work.
In addition to #Juri's answer.. I prefer to keep my Actions empty or as slim as possible. Imagine if you have 3-4 parameters in the Route, you will end up with something like:
public function indexAction($param1 = false, $param2 = false, $param3 = false....)
Here is how I prefer to handle Route parameters:
public function indexAction()
{
// All parameters
print_r($this->dispatcher->getParams());
// Accessing specific Named parameters
$this->dispatcher->getParam('id');
$this->dispatcher->getParam('language');
// Accessing specific Non-named parameters
$this->dispatcher->getParam(0);
$this->dispatcher->getParam(1);
...
}

Specifyng a default message for Html.ValidationMessageFor in ASP.NET MVC4

I want to display an asterisk (*) next to a text box in my form when initially displayed (GET)
Also I want to use the same view for GET/POST when errors are present) so For the GET request
I pass in an empty model such as
return View(new Person());
Later, when the form is submitted (POST), I use the data annotations, check the model state and
display the errors if any
Html.ValidationMessageFor(v => v.FirstName)
For GET request, the model state is valid and no messages, so no asterisk gets displayed.
I am trying to workaround this by checking the request type and just print asterisk.
#(HttpContext.Current.Request.HttpMethod == "GET"? "*" : Html.ValidationMessageFor(v=> v.FirstName).ToString())
The problem is that Html.ValidationMessageFor(v=> v.FirstName).ToString() is already encoded
and I want to get the raw html from Html.ValidationMessageFor(v=> v.FirstName)
Or may be there is a better way here.
1. How do you display default helpful messages (next to form fields) - such as "Please enter IP address in the nnn.nnn.nnn.nnn format) for GET requests and then display the errors if any for the post?
2. What is the best way from a razor perspective to check an if condition and write a string or the MvcHtmlString
Further to my last comment, here is how I would create that helper to be used:
public static class HtmlValidationExtensions
{
public static MvcHtmlString ValidationMessageForCustom<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string customString)
{
var returnedString = HttpContext.Current.Request.HttpMethod == "GET" ? customString : helper.ValidationMessageFor(expression).ToString();
return MvcHtmlString.Create(returnedString);
}
}
And it would be used like this #Html.ValidationMessageForCustom(v=> v.FirstName, "Please enter IP address in the nnn.nnn.nnn.nnn format")

Request and Response Headers Override using Restler

I am new to restler and trying to do the following things, can't seem to get hold of it
I have this class and method exposed via Restler
class Account {
protected $Api_Version = array('version' => "1.0.2.1234", 'href' => "/");
// Returns the version of the service
// Content-Type: application/vnd.cust.version+json
function version() {
return json_encode($this->version);
}
// Accepts only Content Type: application/vnd.cust.account+json
function postCreate() {
}
}
1) I want to return my own Content-Type to client like in the 'version' method instead of default application/json. In my case its 'application/vnd.cust.version+json'
2) Method postCreate should only accept the request if the Contet-Type is set to 'application/vnd.cust.account+json'. How to check if that header is set in the request.
3) Also in the restler API Explorer, for methond name, how can I show only the method name instead of the 'version.json'. I want to show just 'version' like the method name
Thank you for your help.
Narsi
1) maybe Write your own format? Take a Look at
http://restler3.luracast.com/examples/_003_multiformat/readme.html
2) you could check the headers and throw Exception on wrong one.
Take a Look at this link
http://stackoverflow.com/questions/541430/how-do-i-read-any-request-header-in-php
3) have you tried to and the following line to your Index.php?
Resources::$useFormatAsExtension = false
Hope takes you further on :)
CU
Inge

send parameters to actionIndex in Yii controllers

I want to send a sql string for index action in Yii controller? something for example:
index.php?r=staff/index&id=1
I tried it and changed actionIndex() to actionIndex($id) but Yii gave me
error 400 : Your request is invalid.
Is it possible or I have to define another action ?
no you don't need to do that, receive the id as the normal request parameter inside your action method
public function actionIndex(){
$id = $_GET['id'];
}
your can get parameter as follows.
$key = Yii::app()->getRequest()->getParam('your_parameter_name');
and also you can get all the parameters from
$allParam = Yii::app()->getRequest()->restParams
OR
actionIndex($id = 0)
in your staff controller