I am referring to the problem that has already been asked. CakePHP 2.0 - How to make custom error pages?
It gives me a lot idea of solving out the problem but instead of thowing exception I want to use it for all of my controller and actions. It suggested me to do where ever at particular location I want throw new NotFoundException(); I want it everywhere I mean where so ever controller or action is missing.
With debug turned off all your error are converted to either 400 or 500 errors. So you just need to customize your app/View/Errors/error400.ctp and app/View/Errors/error500.ctp as required.
Related
I want to create one custom 404 page for all errors coming to the website for production environment. For example if I receive missing controller or view error then it will redirect to
http://example.com/404.html, Also in some cases I will deliberately redirect it http://example.com/404.html
Earlier in CakePHP 2.x it was done by adding following action in AppContoller.php
public function appError($error) {
$this->redirect('/page-not-found.html',301,false);
}
But It is not working in CakePHP 3.x, I want to replicate same behavior in CakePHP 3.x
Do not redirect
The correct action to take if a page should render a 404 is to render a 404.
Redirecting to another page is confusing for users, especially since many browsers cache 301 responses making the original url inaccessible. This also affects e.g. search engines as the static file 404.html will have a 200 response code (unless you modify your webserver config) so it'll say 404 but simply not be.
The blog tutorial, which all devs should do before starting a project, guides you in the right direction:
public function view($id)
{
$article = $this->Articles->get($id);
$this->set(compact('article'));
}
The table method get returns a single entity object or throws an exception:
If the get operation does not find any results a Cake\Datasource\Exception\RecordNotFoundException will be raised. You can either catch this exception yourself, or allow CakePHP to convert it into a 404 error.
The controller code in this example doesn't need to have any "what if it doesn't exist" handling because by default if the record doesn't exist the result is a 404.
Changing the 404 template
If you want to change the way the 404 or 500 pages look change the template files
For all 4xx and 5xx errors the template files error400.ctp and error500.ctp are used respectively.
The error template is in your application, note that in production mode the output is very minimal.
If the get operation does not find any results a Cake\Datasource\Exception\RecordNotFoundException will be raised. You can either catch this exception yourself, or allow CakePHP to convert it into a 404 error.
The simplest way can be just implemented in two steps,
Step 1:
turn the debug mode off in config/app.php
Step 2:
Replace the content of src/Template/Layout/error.ctp with your custom layout.
I do like the NancyFX error custom page. Unfortunately an exigent customer prefer not to see the green monster besides the error. Is there some way to just remove the image from the almost perfect nancyfx error message page?
You implement an IStatusCodeHandler and handle the status codes you want (like 404 and 500) by returning a response for them. If you need to render a view, take a dependency on the IViewRenderer and off you go =)
I was trying to write my own exception handler for CakePHP 2.2 but I totally lost in implementation.
http://book.cakephp.org/2.0/en/development/exceptions.html
So I decided to use MyController::myAction in exceptions. Can you tell me easiest way to redirect users to myAction and parse error type inside action.
According to the documentation you following, it looks you were on the right track.
You can create your own custom controller to handle exception. See this link: http://book.cakephp.org/2.0/en/development/exceptions.html#creating-a-custom-controller-to-handle-exceptions
My last post can be found here,
CakePHP Issue : Call to a member function find() on a non-object
I am asking a new question, as the people who answered my last question, said that the error I am getting now is a new problem, therefore a new post.
Please read my last post, if any of you wish me to re-post all my code on this post I will do so, just ask!
So my problem is that, yes once I do remove the vars $uses from my code, it works in the NEW copy of cake I have put on the same server. However it just bypasses my die command when I use it on my live site! Now I know (or at lest think) this must be something to do with the way this copy of cake was configured.
Also, in the NEW copy of cake I did not have to add a 'Router' path in the routes.php file for it to work. However on the live site, it will just give me a 404 error when trying to access the controller. Now with the NEW copy of cake, I called the function index(), but in the live site I named it eventDetails(), now I have tried changing this to index(), but I still get a 404 error when the router path is not there and the same 'Internal Error Has Occurred', see last post.
Any ideas?
Again any help would be very welcome.
Glenn Curtis.
Thanks For your help Dunhamzz.
But I have solved the issue. I knew it was something to do with my set-up and the guy who set-up this cake site set the debug level to 0. While the new copy of cake had it set to 2.
Thanks
Glenn.
Iam trying to store the state in thedb through webservice.
Iam using Http State Provider to do so.
Iam getting an error in the following line saying object expected.
Ext.state.Manager.setProvider(new Ext.state.HttpProvider({ url: 'GetGridState.asmx/readdata' }));
Please help me in this issue.
You should probably use firebug or even alerts if nothing else to see if maybe you're not passing an object where you should be.
Are you using the correct version of ExtJS? It looks like there is no more Ext.state.HttpProvider in ExtJS 3.1.
Sounds like you have not included the HttpStateProvider class code correctly, or included it after the code that is trying to use it. Double-check that if you are referring to an external js file, it is loaded properly and in the right order.