Fat Free Framework (F3): custom 404 page (and others errors) - php

How I can handle my 404 custom page (and possibly other errors)?
I just tried in routing part to add
GET /#codes /WebController->error
Where my Class WebController handles error, and for 404 i solved (partially). In effect it works for
http://mydomain.ext/itdoesntexists
but if i recall a
http://mydomain.ext/sub/maybe_another_sub/and_so_on/doesnt_exist
My route (of course) doesn't work.
Btw, with that route in every case it doesn't push 404 header (just a maniac-vision of things, i'm thinking to Google looking for a resources and it doesn't receive a "pure" 404).
Thank you

You don't have to define a route for this. F3 will automatically generate a 404 status code for any non-defined route.
If you want to define a custom error page, you need to set the ONERROR variable.
Here's a quick example:
$f3->route('GET /','App->home');
$f3->set('ONERROR',function($f3){
echo \Template::instance()->render('error.html');
});
$f3->run();
with error.html defined as:
<!DOCTYPE html>
<head>
<title>{{#ERROR.text}}</title>
</head>
<body>
<h1>{{#ERROR.text}}</h1>
<p>Error code: {{#ERROR.code}}</p>
</body>
</html>
Now if you call any non-defined route like /foo, the template error.html will be rendered with a 404 status code.
NB: this works for other error codes. Other error codes are triggered by F3 or your application with the command $f3->error($status), $status being any valid HTTP status code (404, 500, 403, etc...)

Related

Return Redirect with body in Laravel

I'm trying to do a redirect with body in Laravel. I've tried this:
return Redirect::to($log_in_url)->with('body','<html><head><title>Redirecting to your page</title></head><body>If you are not redirected within 5 seconds, please cl ick here.</body></html>');
I look in the network tab, I don't really see anything.
The question is that how would one make a delayed redirection by showing an HTML waiting page before the actual redirection happens?
You're making a handful of false assumptions:
The with method puts the thing into the session so that you can access it after the redirection. A common usecase is to set messages and then redirect the user.
Don't expect magic by just setting the thing body.
There's no such a standardized redirection called "redirect with body" as you stated. If you need such a thing, you have to implement it.
I assume you're having one of those vBulletin-like redirect styles in mind. To implement it in Laravel context, you gonna need a mediatory view to do a clientside redirect for you after a set amount of delay. Let's name it redirect.blade.php:
<!doctype html>
<html lang="en">
<head>
<title>Redirecting...</title>
<script>
window.setTimeout(function () {
window.location = "{{ $url }}";
}, 5000);
</script>
</head>
<body>
If you are not redirected within 5 seconds,
please click here.
</body>
</html>
With this in place, your controller will pass a $url to this mediatory view and let it be rendered to do the clientside redirection:
# Controller method
return view('redirect', ['url' => $log_in_url])
This style of redirection won't be working if JavaScript is disabled and that's why they put a link into the page content and warn the user about it.
Some take a hybrid approach:
<noscript>
<meta http-equiv="refresh" content="5;url={{ $url }}" />
</noscript>
The reason that they don't go with the refresh header/meta tag in the first place is that it's not specified in the HTTP standard. Read more.
I strongly suggest that you look into alternatives. This is so 1990 and not user-friendly at all.
As a visitor, if I deal with a website that makes me wait for 5 godddamn seconds, I'd just leave. That's why people used to make browser extensions to workaround the vBulletin's login screen waiting time!
Embrace simplicity and just do a regular HTTP redirect. It's best for all humanity.
It's not a task for Laravel. You can just return page with meta, or using javascript.
// Controller
return view('redirect');
// View redirect.blade (Regular html page with additional meta tag)
<meta http-equiv="refresh" content="5;url=http://www.google.com/" />

Included link tags don't load css

I am not sure what exactly is broken and it's kinda hard to explain. My file structure looks something like this:
+bin
+css
-style.css
-fontawesome.min.css
+fonts
-FontAwesome.otf
- ...
+www
+comp
-header.php
-footer.php
-index.php
-otherpage.php
Both index.php and otherpage.php include header.php and footer.php at the top and bottom, the content is in between.
Requesting the domain gives me the index.php and requesting the otherpage.php shows that as well as expected.
Requesting the configured bin.domain.com/css/style.css shows the correct css-file.
However, and this is the problem: The Link-Tag to the css-files is in the header.php and the css is not applied. Going to the Inspector shows the link-tag and following the link inside that shows the css that should apply to the page.
Now, here comes the weird thing: Going to the Style-Editor Tab in the Firefox Devtools shows the CSS from fonts.google.com as one link-tag imports a font. This is as expected but nothing else is shown so I don't even know where the error is at. Any Ideas?
Requesting the configured bin.domain.com/css/style.css shows the correct css-file.
This might not be it but you say you are using the following
<link href="bin.domain.com/css/style.css" rel="stylesheet">
What if you change it to
<link href="http://bin.domain.com/css/style.css" rel="stylesheet">
I found the solution:
The website itself was loaded via https. The Stylesheets and assets however where linked with http. I regenerated the SSL-Certificate and made the bin-subdomain a https-one. This was also said in the console in my Firefox-Browser but I didn't notice that.
This also explains why the Google-Font-Link worked while the others didn't. It was a https-request instead of a http-one.

Lumen's url() function performing strangely

I recently did a fresh install of Lumen framework and started building a site from it. Yes I know lumen is designed only for APIs but please help me out with this.
I have placed all my views inside /resources/views and my templates inside /resources/views/templates.
Now since I had to place [css/js/images] somewhere, I thought placing all of that in public/assets/[css/js/images] would be nice.
Now in my layout when I am trying to include css - I use something like this -
<link href="{{ url('assets/css/something.css') }}">
and it works giving an output of something like this -
<link href="localhost/assets/css/something.css">
same thing works for js also but it gets strange when I try to include images. For including an image I write something like -
<img src ="{{ url('assets/images/someimage.jpg') }}"
and when I view page source, output is as I expect it to be -
<img src="localhost/assets/images/someimage.jpg">
but my console fires 404 not found errors stating someimage.jpg not found. And when I crosscheck by inspecting the image's parent, the Url is totally different soemthing like this -
<img src="localhost/images/someimage.jpg">
See, automatically omitting 'assets' from image url and resulting in 404, but I can see correct url when I view page source.
Things I tried to resolve the issue -
Cleared cache and reloaded the page.
Tried using asset() instead of url() but prototype of that was removed from lumen.
Pull out [css/js/images] folder from assets and pasted them in parent i.e. public. This worked but then the question is why did the previous setup worked find for both css and js and caused problem only with images ?
My other questions are -
1. How can the url in page source be different from the one being rendered ? Just to mention in my case the url in page source worked and displayed image but since the url being renderred omitted 'assets' from path hence resulted in 404.
2. Is there any other good way to include these assets in views. If yes then please also mention where to put them ?
Attaching some images/code for reference.
Output of rendered page showing 404 errors for images but none for css.
Output of view page source windows showing asset included in image path
No clue if this is right, but I believe you actually need to put an image stream inside that URL. Now the server is trying to retrieve some byte encoded object that isn't there.
I have no idea if it's the same case for you, but I've had many instances where I had to put in image streams instead of URLs, which I've solved this way using this library:
Routes.php
/**
* Image handling routes.
* These make sure images actually are images instead of bytecode
*/
Route::get('organization/logo/{logo}', function($logo) {
$file = Image::make(Storage::disk('logo-image')->get($logo));
return $file->response();
});
View
<img src="{{ asset('organization/logo/' . $organization->logo_path) }}" alt="">
I might be completely off, but I recognize what's happening with your app and this took care of the issues when I implemented it.
Check your web server configuration. It sounds like you have some type of redirect setup that redirects assets/images/* to just images/*.
As a simple test, open your "Network" tab and navigate your browser to http://samplelumena.local/assets/images/footer1.jpg. I'm guessing the Network trace will show a 30x (301, 302, etc.) to http://samplelumena.local/images/footer1.jpg, followed by the 404 for that url.

Append stylesheet with Zend Framework only works if no action is specified

I started using Zend Framework and tried to add some stylesheets and javascript files like this in my layout.phtml:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LoremIpsum</title>
<?php echo $this->headLink()->appendStylesheet('css/global.css'); ?>
</head>
The problem with this is, that it is only working if there is no action specified in the url.
For example it works with this:
www.domain.com, www.domain.com/index, www.domain.com/login
but if I add the action, even if it is
www.domain.com/index/index
it stops working.
I noticed that in this case the index.php is called twice and calls the ErrorController.
But the output shows no error. It is the expected output just without the stylesheet information.
Any ideas what is wrong with my layout?
Edit 1:
Error Reporting is activated and works in other cases.
HTML markup is correct.
Firebug show that my css file wasn't found, but the path can't be wrong because without specified action it works correctly.
You can use the BaseUrl() View helper:
<?php echo $this->headLink()->appendStylesheet($this->baseUrl().'/css/global.css'); ?>
Then it should be fine...
If you want to see the erros, you will have to change your working envirement from production to developement (this is done by adding SetEnv APPLICATION_ENV development to your .htaccess file)
About the layout randering twice, add this to the errorAction in the errorController file (make it the first line of the action) : $this->getResponse()->clearBody();
Edit :
Ok, Try this for appending the stylesheet instead :
$this->headLink()->appendStylesheet('css/global.css');
echo $this->headLink();

Display a view for 404 error in CodeIgniter

The CodeIgniter has a very simple default error 404 message:
404 Page Not Found
The page you requested was not found.
Instead of using this error message on totally blank page, I want to wrap this message in between my header and footer view, so that the error message have similar look to the other pages.
For that purpose, I have created an error view? For example:
my404_view.php
<? $this->load->view('header'); ?>
404 Page Not Found
The page you requested was not found.
<? $this->load->view('footer'); ?>
Now, How can I use this my404_view.php as a default view to display 404 messages instead of using the CodeIgniter default error message.
You should change your routes.php. For example:
in application/config/routes.php
$route['404_override'] = 'welcome/_404';
in application/controllers/welcome.php
function _404(){
$this->load->view("my404_view");
}
And this should be sufficient in the current version of CI.
Including headers and footers in the default error 404 page seems to be a common problem for CodeIgniter users. I would like to add this link: Simon Emms's comments at http://www.simonemms.com/2011/05/06/codeigniters-404-override-problem/ because he describes the problem so clearly.
I have tried the suggestions at http://maestric.com/doc/php/codeigniter_404 and played around with Simon Emms's ideas, and others, but just can't implement them. I'm a bit of a novice at PHP and CodeIgniter, so that might be because of ignorance. That said, it is difficult to ensure that you put the suggested subclasses in the right places and configure, for instance, routes.php correctly. After 3 days of trying the various rather complicated ideas, it occurred to me that I could just use an include statement in the default error 404 page. The only difficulty was figuring out the path to pass to the include statement.
In /system/core/Exceptions.php line 146, I found the CodeIgniter developers use APPPATH. You then just have to append the path to the header and footer pages you want to include.
My new default error 404 page now looks like this:
<?php
include APPPATH.'/views/templates/header.php';
?>
<div id="container">
<h1><?php echo $heading; ?></h1>
<?php echo $message; ?>
</div>
<?php
include APPPATH.'/views/templates/footer.php';
?>
This seems a much easier solution to me than trying to change the core classes in CodeIgniter.
There is quite a bit of information on this.
http://maestric.com/doc/php/codeigniter_404
http://www.nickyeoman.com/blog/apache/90-htaccess-404-page
http://hasitha.posterous.com/customising-error-pages-on-codeigniter (archived)

Categories