cakephp url append invalid parameter But still work. Should be 404 page not found - cakephp

I find a problem when i develop application via cakephp.
for example: my url is http://localhost/controller/view/id this is working fine.
BUT, when i append more invalid parameter, it still works,
like http://localhost/controller/view/id/adfasd/adfasdf/asdfasdf/asdfasdf
It should show up 404 page not found.
Shall i need to use $this->passedArgs to check pass parameter manually in controller then throw exception? Or is there any configuration?
How can i deal with this case
Thank you

You should first look here Cakephp, Routing-Named params to find out how to properly use them.
As you should add which one to use, you should also add a regex to your id in the route.
Also when sending the data to an action you should throw the exception there like it is explained here: cakephp deal with passing wrong parameter in url

Related

AngularJS $http service has CORS issue. But it should be working for JSONP, right? [duplicate]

I'm trying to load an external page using JSONP, but the page is an HTML page, I just want to grab the contents of it using ajax.
EDIT: The reason why I'm doing this is because I want to pass all the user information ex: headers, ip, agent, when loading the page rather than my servers.
Is this doable? Right now, I can get the page, but jsonp attempts to parse the json, returning an error: Uncaught SyntaxError: Unexpected token <
Sample code:
$.post('http://example.com',function(data){
$('.results').html(data);
},'jsonp');
I've set up a jsfiddle for people to test with:
http://jsfiddle.net/8A63A/1/
http://en.wikipedia.org/wiki/JSONP#Script_element_injection
Making a JSONP call (in other words, to employ this usage pattern),
requires a script element. Therefore, for each new JSONP request, the
browser must add (or reuse) a new element—in other words,
inject the element—into the HTML DOM, with the desired value for the
"src" attribute. This element is then evaluated, the src URL is
retrieved, and the response JSON is evaluated.
Now look at your error:
Uncaught SyntaxError: Unexpected token <
< is the first character of any html tag, probably this is the start of <DOCTYPE, in this case, which is, of course, invalid JavaScript.
And NO, you can't use JSONP for fetching html data.
I have done what you want but in my case I have control of the server side code that returns the HTML.
So, what I did was wrapped the HTML code in one of the Json properties of the returned object and used it at client side, something like:
callback({"page": "<html>...</html>"})
The Syntax error you are facing it's because the library you're using expects json but the response is HTML, just that.
I've got three words for you: Same Origin Policy
Unless the remote URL actually supports proper JSONP requests, you won't be able to do what you're trying to. And that's a good thing.
Edit: You could of course try to proxy the request through your server …
If you really just want to employ the client to snag an HTML file, I suggest using flyJSONP - which uses YQL.. or use jankyPOST which uses some sweet techniques:
jankyPOST creates a hidden iframe and stuffs it with a form (iframe[0].contentWindow.document.body.form.name).
Then it uses HTML5 (watch legacy browsers!) webMessaging API to post to the other iframe and sets iframe's form elements' vals to what u specified.
Submits form to remote server...done.
Or you could just use PHP curl, parse it, echo it, so on.
IDK if what exactly ur using it for but I hope this helps.
ALSO...
I'm pretty sure you can JSONP anything that is an output from server code. I did this with ClientLogin by just JSONPing their keyGen page and successfully consoleLogged the text even though it was b/w tags. I had some other errors on that but point is that I scraped that output.
Currently, I'm trying to do what you are so I'll post back if successful.
I don't think this is possible. JSONP requires that the response is rendered properly.
If you want another solution, what about loading the url in an iframe and trying to talk through the iframe. I'm not 100% positive it will work, but it's worth a shot.
First, call the AJAX URL manually and see of the resulting HTML makes sense.
Second, you need to close your DIV in your fiddle example.

Query parameter string in cakePHP 3.4

I'm getting null query parameter string even though passing query parameter string like this login?redirect=%2Farticles%2Fadd.
I have tried with $this->request->getQuery('redirect'); and $this->request->query('redirect'); but no luck.
Does anyone have any idea that where it would be wrong?
Looks like you are not setup your URL Route properly in your config/routes.php file. When you are interested to pass any route parameter to your controller action you should configure your route proper way. Although your question is not written detailedly.
I hope you will get much more information from this articles.
Passing Parameters to Action

Passing parameters to ->redirect() via $_GET

Hi I have this sentence
$g->addButton('')->set('NEW ACTIVITY')->js('click')->univ()->redirect('newactivity');
Is it possible to call the "redirect" method and passing parameters via $_GET ? so in the page "newactivity" I can ask for $_GET['something'] ?
Something like this
$g->addButton('')->set('NEW ACTIVITY')->js('click')->univ()->redirect('newactivity?id=1'); (this doesn't work)
or
$g->addButton('')->set('NEW ACTIVITY')->js('click')->univ()->redirect('newactivity','id=1');
Thanks
What you need is to properly build destination URL.
http://agiletoolkit.org/learn/understand/page/link
->univ()->redirect($this->api->getDestinationURL('newactivity',array('id'=>1)));
using stickyGET will affect ALL the urls you are going to produce form this point on. So if you add 2 links, each of them would be passing ID.
stickyGET is better if you need to pass argument which was already passed through GET, such as
array('id'=>$_GET['id']);
Here is a place where other ATK4 Developers chat too, perhaps another resource for your ATK4 Q's. https://chat.stackoverflow.com/rooms/2966/agile-toolkit-atk4

Controller Redirect issue in CakePHP

I have a controller with name users_controller, within the login action I want to redirect to my affiliate_redirect_controller.php, now I the following code in the users controller to redirect
$this->redirect(array(
'controller'=>'affiliate_redirect',
'action'=>'logRedirect' ));
And then I get the following error which I can't seem to resolve
Error: The requested address '/affiliate_redirect/logRedirect' was not found on this server.
I honestly do not know what this could be, quite new to cakePHP and none of the solutions found work for me.
the contents of affiliate_redirect_controller.php looks like this
class AffiliateRedirectController extends AppController
{
var $name = 'AffiliateRedirect';
function logRedirect(){
}
}
I can see there is a mistake in your code its because of naming convention.
$this->redirect(array(
'controller'=>'affiliate_redirects',
'action'=>'logRedirect' ));
Please see the above changes when you are writing your controller name in lowercase like above it should be plural affiliate_redirects and should not be affiliate_redirect
Apart from this you can use directly redirect as like this also.
$this->redirect('affiliate_redirects/logRedirect');
Please try, it should work.
Do you have a table in your database that corresponds to affiliate redirect controller?
You might want to rethink your logic, and use CakePHP routes to set the URL to what you want. Having a controller named affiliate_redirect_controller doesn't follow CakePHP's naming conventions.
Since I don't know exactly what you're trying to do, I don't know if this will work for you, but maybe consider redirecting to a separate action in UsersController like /users/affiliate_redirect/
Or you can create an AffiliatesController and then redirect to /affiliates/redirect/
Also, if you don't have debug mode set to 2, you should do that. It may help reveal what the actual issue is.
What debug level do you have in app/config/core.php ? Most of the time, when you get the message
Error: The requested address '/controller/action' was not found on this server.
it means you have a debug level set to 0 and increasing it to 1 or 2 allows to get more details about the error.

cakephp and get requests

How does cakephp handle a get request? For instance, how would it handle a request like this...
http://us.mc01g.mail.yahoo.com/mc/welcome?.gx=1&.rand=9553121_pg=showFolder&fid=Inbox&order=down&tt=1732&pSize=20&.rand=425311406&.jsrand=3
Would "mc" be the controller and "welcome" be the action?
How is the rest of the information handled?
Also note that you could use named parameters as of Cake 1.2. Named parameters are in key:value order, so the url http://somesite.com/controller/action/key1:value1/key2:value2 would give a a $this->params['named'] array( 'key1' => 'value1', 'key2' => 'value2' ) from within any controller.
If you use a CNN.com style GET request (http://www.cnn.com/2009/SHOWBIZ/books/04/27/ayn.rand.atlas.shrugged/index.html), the parameters are in order of appearance (2009, SHOWBIZ, books, etc.) in the $this->params['pass'] array, indexed starting at 0.
I strongly recommend named paramters, as you can later add features by passing get params, without having to worry about the order. I believe you can also change the named parameter separation key (by default, it's ':').
So it's a slightly different paradigm than the "traditional" GET parameters (page.php?key1=value1&key2=value2). However, you could easily add some logic in the application to automatically parse traditional parameters into an array by tying into how the application parses requests.
CakePHP uses routes to determine this. By default, the routes work as you described. The remainder after the '?' is the querystring and it can be found in $this->params['url'] in the controller, parsed into an associative array.
Since I found this while searching for it, even though it's a little old.
$this->params['url']
holds GET information.
I have tested but it does work. The page in the Cakephp book for it is this link under the 'url' section. It even gives an example very similar to the one in the original question here. This also works in CakePHP 1.3 which is what I'm running.
It doesn't really use the get in the typical since.
if it was passed that long crazy string, nothing would happen. It expects data in this format: site.com/controller/action/var1/var2/var....
Can someone clarify the correct answer? It appears to me that spoulson's and SeanDowney's statements are contradicting each other?
Would someone be able to use the newest version of CakePHP and get the following url to work:
http://www.domain.com/index.php/oauth/authorize?oauth_version=1.0&oauth_nonce=c255c8fdd41bd3096e0c3bf0172b7b5a&oauth_timestamp=1249169700&oauth_consumer_key=8a001709e6552888230f88013f23d5d004a7445d0&oauth_signature_method=HMAC-SHA1&oauth_signature=0bj5O1M67vCuvpbkXsh7CqMOzD0%3D
oauth being the controller and authorize being a method AS WELL as it being able to accept the GET request at the end?

Resources