I need some background information about CakePHP, and how it works...
let's say that i have method (function) defined in CakePHP's controller, ie. deleteItem, like
function deleteItem( $id = null )
{
$this->PublicationNumeration->delete( $id, true );
}
The CMS I developed works ok, proper record is deleted ($id), and it works fine.
But if I try to call this method from browser, I am getting the error 'page not found'.
Is it possible to skip that error (no matter how)?
For development mode, set debug value to 2 in app/config/core.php:
Configure::write('debug', 2);
If you set debug value to 2, you can get detailed message of what happening. From your description, there are two possibilities:
record with given id has been deleted, or
view from current action is not exist. Usually in delete action, you
don't create view but redirect it to
somewhere else (which you not do in
code above).
If you want, you can create your own custom error pages in /views/errors
Make sure you're including the controller's name in the URL as well. If your controller's class name is CategoriesController and you want to delete an item with an ID of 4, for example, make sure you're going to this address in the browser:
http://www.example.com/categories/deleteItem/4
The page is not found because you haven't created it (deleteItem.ctp). After the delete statement, put a redirect to the page you want to return to, usually an index page after a delete operation.
Related
Since whole a day I have been trying to search for best solution to redirect to same page where it is requested. Means I have a edit action page. I can move on this page from anywhere either it is detail, view, latest, recent or from any other action I can move to edit action. What I want is I want to move to last action again where it is redirected from. While googling I got solution to put
`$this->redirect($this->referer());` //or
`$this->redirect(Controller::referer());`
to use in controller to move to but it reverts to edit page again as it is navigated from that only.
Other solution told me to use hidden variable whose value it
`echo $this->Form->input('Song.referer', array('type' => 'text', "value"=>$this->request->referer()));`
like this and request to this variable on successfull edit that is OK. But it fails in case of validation put on controller as same page is open in case of error so my referer change. What shall be best practise in this case. Should I save session in and every call to main my referrer or something other available with cakephp 2.3
I use this method:
when I create a link to my edit page in my action i add a 'referer' param
$this->Html->link('Add', array('action' => 'add', 'referer' => 'detail'));
in my controller then I read the value of referer and i redirect to the right page after saving the data, even in case of validation error the value of referer remains in the url of the page
It's not a great solution since it's not transparent to the user who see the url and can change the referer changing the url itself. But it works and it's simple to implement
Save the last location in hidden field eg.
$this->request->data["Business"]["referer"] = $this->referer();
Now put optional condition on save function to redirect on the page like this.
if ($this->Business->save($this->request->data)) {
(isset($this->request->data["Business"]["referer"]) && !empty($this->request->data["Business"]["referer"]) ? $this->redirect($this->request->data["Business"]["referer"]) : $this->redirect(array("action" => "index"));
}
My AngularJS application needs to have access to the user's LinkedIn profile. In order to do that I need to redirect the user to a LinkedIn URL which contains a callback redirect_uri parameter which will tell LinkedIn to redirect the user back to my webapp and include a "code" query param in the URL. It's a traditional Oauth 2.0 flow.
Everything works great except that LinkedIn redirects the user back to the following URL:
http://localhost:8080/?code=XXX&state=YYY#/users/123/providers/LinkedIn/social-sites
I would like to remove ?code=XXX&state=YYY from the URL in order to make it clean. The user does not need to see the query parameters I received from LinkedIn redirect.
I tried $location.absUrl($location.path() + $location.hash()).replace(), but it keep the query params in the URL.
I am also unable to extract the query parameters, e.g. "code", using ($location.search()).code.
It seems like having ? before # in the URL above is tricking Angular.
I use
$location.search('key', null)
As this not only deletes my key but removes it from the visibility on the URL.
I ended up getting the answer from AngularJS forum. See this thread for details
The link is to a Google Groups thread, which is difficult to read and doesn't provide a clear answer. To remove URL parameters use
$location.url($location.path());
To remove ALL query parameters, do:
$location.search({});
To remove ONE particular query parameter, do:
$location.search('myQueryParam', null);
To clear an item delete it and call $$compose
if ($location.$$search.yourKey) {
delete $location.$$search.yourKey;
$location.$$compose();
}
derived from angularjs source : https://github.com/angular/angular.js/blob/c77b2bcca36cf199478b8fb651972a1f650f646b/src/ng/location.js#L419-L443
You can delete a specific query parameter by using:
delete $location.$$search.nameOfParameter;
Or you can clear all the query params by setting search to an empty object:
$location.$$search = {};
At the time of writing, and as previously mentioned by #Bosh, html5mode must be true in order to be able to set $location.search() and have it be reflected back into the window’s visual URL.
See https://github.com/angular/angular.js/issues/1521 for more info.
But if html5mode is true you can easily clear the URL’s query string with:
$location.search('');
or
$location.search({});
This will also alter the window’s visual URL.
(Tested in AngularJS version 1.3.0-rc.1 with html5Mode(true).)
Need to make it work when html5mode = false?
All of the other answers work only when Angular's html5mode is true. If you're working outside of html5mode, then $location refers only to the "fake" location that lives in your hash -- and so $location.search can't see/edit/fix the actual page's search params.
Here's a workaround, to be inserted in the HTML of the page before angular loads:
<script>
if (window.location.search.match("code=")){
var newHash = "/after-auth" + window.location.search;
if (window.history.replaceState){
window.history.replaceState( {}, "", window.location.toString().replace(window.location.search, ""));
}
window.location.hash = newHash;
}
</script>
If you want to move to another URL and clear the query parameters just use:
$location.path('/my/path').search({});
Just use
$location.url();
Instead of
$location.path();
If you are using routes parameters just clear $routeParams
$routeParams= null;
How about just setting the location hash to null
$location.hash(null);
if you process the parameters immediately and then move to the next page, you can put a question mark on the end of the new location.
for example, if you would have done
$location.path('/nextPage');
you can do this instead:
$location.path('/nextPage?');
I've tried the above answers but could not get them to work. The only code that worked for me was $window.location.search = ''
I can replace all query parameters with this single line: $location.search({});
Easy to understand and easy way to clear them out.
The accepted answer worked for me, but I needed to dig a little deeper to fix the problems with the back button.
What I noticed is that if I link to a page using <a ui-sref="page({x: 1})">, then remove the query string using $location.search('x', null), I don't get an extra entry in my browser history, so the back button takes me back to where I started. Although I feel like this is wrong because I don't think that Angular should automatically remove this history entry for me, this is actually the desired behaviour for my particular use-case.
The problem is that if I link to the page using <a href="/page/?x=1"> instead, then remove the query string in the same way, I do get an extra entry in my browser history, so I have to click the back button twice to get back to where I started. This is inconsistent behaviour, but actually this seems more correct.
I can easily fix the problem with href links by using $location.search('x', null).replace(), but then this breaks the page when you land on it via a ui-sref link, so this is no good.
After a lot of fiddling around, this is the fix I came up with:
In my app's run function I added this:
$rootScope.$on('$locationChangeSuccess', function () {
$rootScope.locationPath = $location.path();
});
Then I use this code to remove the query string parameter:
$location.search('x', null);
if ($location.path() === $rootScope.locationPath) {
$location.replace();
}
I was setting up this application in cakephp.I have homes controller which works well and then i have chokates controller which has an index action.But whenever i run this chokates controller i get an error
Call to a member function charset() on a non-object
I had a print of $this in top of this controller which shows me that html and javascript helpers are loaded already.Then why i get this error i dont know.
Please help
Here is the link http://www.maninactionscript.com/chokate
go to the chocolates link.
Regards
Himanshu Sharma
Well, looking at your error message it appears that a couple things are happening here that should be fixed:
The view for ChokateController::index() was not found.
Ensure that you have an index.ctp file in /app/views/chokate/.
The HtmlHelper object likely isn't being added to the view because you have a manually configured list of $helpers. If you have assigned an array to $helpers in ChokateController ensure that Html is listed.
I am attempting to use the auth plugin Spark Plug on a new CakePHP 1.3 app at http://sandbox.andrewcroce.com. It is easy enough to set up, but for some reason I am getting redirect loop errors when trying to access anything other than the Users controller.
The plugin successfully allows you to register and login, the database appears to be written correctly. Confirmation emails are sent, and the verification link seems to activate a new user. However I am unable to access any page or controller, other than the Users controller. The result is a redirect loop where http://sandbox.andrewcroce.com/errors/unauthorized is repeatedly requested.
For me this raises 2 questions: if I am logged in successfully, why is it trying to direct me to the unauthorized page? and why the heck does it keep redirecting to iself?
I wonder if this is a configuration setting I am not understanding in the spark plug config, but there isn't much explanation in the comments about what these settings do.
Any help would be appreciated.
I'm not sure about the Spark Plug specifics, but whenever you activate the Auth component you need to make sure you specify which actions are allowed for non-authenticated users, or else any requests for the action will be redirected to whatever your error action is. And then, if you error action isn't allowed, it will throw an error, sending you to... you guessed it, your error action, over and over.
Inside every controller, you need something inside your beforeFilter() method like this:
function beforeFilter() {
parent::beforeFilter();
// Allow all actions
$this->allow(*);
// Only allow view and index
$this->allow('view', 'index');
}
If you don't run the allow() method, you're saying that none of the actions should be available to non-authenticated users. In particular, if you put allow('unauthorized') in your ErrorsController class, the unauthorized action wouldn't redirect in a loop.
There is a table called "user_group_permissions" on spark_plug, for instance if you want to access a controller nameed "posts" and the action "sortBy" (http://localhost/posts/sortby/) then you need to add that permission to the table like this:
INSERT INTO `user_group_permissions` ( `user_group_id`, `plugin`, `controller`, `action`, `allowed`) VALUES
( 3, '', 'posts', 'sortBy', 1)
For this specific case the user_group_id number 3 is "Guest", in other words everybody will be able to access that action in the controller
I'm new to CakePHP but I've been though their FAQs and guides to no avail. This is so simple that I just must not be thinking straight:
How can I access a parameter sent through the URL within my view files?
Example: http://example.com/view/6
How would I take that parameter ("6") and cycle it through the controller to another view page?
If that's too complex for a quick answer, how can I reference the 6 within the view page itself? The 6 in this situation is the "Id" value in my database, and I need to set it as the "parent" -
Thanks
Parameters can be retrieved like this
$this->params['pass']
Returns an array (numerically indexed) of URL parameters after the Action.
// URL: /posts/view/12/print/narrow
Array
(
[0] => 12
[1] => print
[2] => narrow
)
To access the parameter in your view look in $this->params
The URL, as you have it, will call the 6() method of your ViewController, which is not a valid method name. You may have to play with your routes to make that work.
If you don't want to configure your routes, you'll need the controller in the URL, like so:
http://example.com/thinger/view/6
which will call thingerControllerObject->view("6"). If you want "/view/" to go to a different method, edit the routes. See:
Cake controllers
Cake routes
Use the code below in the view file :
$url=Router::url($this->here, true);
$url_arr=explode("/",$url);
To see the content of $url been exploded simply print it using pr() as below :
pr($url_arr);
It will print associative array, thus you can access any particular number of parameter sent via url.