I have the following code inside my app/start/global.php file:
App::missing(function($exception){
return Response::view('missing', array('url' => Request::url()), 404);
});
And I have a file called missing.blade.php at app/view. However, the above code gives me a FatalErrorException, telling me "Call to a member function getPath() on a non-object". The formatting of the otherwise pretty error page that I got the above information from is also messed up.
But everything works fine when I change my code in global.php to the following:
App::missing(function($exception){
return Response::make("Page not found", 404);
});
I don't get what's going on. How can I display a specific view when a route is not found?
EDIT:
Here is my missing.blade.php file:
#extends('master')
#section('header')
<h2>404 Error</h2>
#stop
#section('content')
<p>
Unable to locate page.
</p>
#stop
EDIT 2:
Also inside my app/views folder, I have a master.blade.php file with the following structure:
...
#yield('header')
...
#yield('content')
...
The following should work for you and gives you more control on other errors as well:
App::error(function(Exception $exception, $code)
{
Log::error($exception);
switch ($code)
{
case 403:
return 'Your 403 message';//or load any custom view
break;
case 404:
return Redirect::to('/');//or load any custom view
break;
case 500:
return 'Your 500 Error Message';//or load any custom view
break;
default:
return Redirect::to('/');//or load any custom view
}
});
Related
When i wrote this in command line composer require brian2694/laravel-toastr. I've got message like [InvalidArgumentException]
Could not find package brian2694/laravel-toastr. It was however found via repository search, which indicates a consistency issue with the repository.
Any tips to solve this problem ?
First thing, you need to include the Toastr library to Laravel blade views. You can achieve this by below methods.
1.Include css and js file from a CDN
2.Install the library using NPM
3.Download the css and js file from github Toastr Repository
However you choose the grab the library, you have to add the location to the 2 files in the head tag of your master layout file like so:
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/2.1.4/toastr.min.css">
After including the library, whenever you need to redirect to a page most probably from a controller, you can pass the notification information like so:
$notification = array(
'message' => 'Post created successfully!',
'alert-type' => 'success'
);
return Redirect::to('/')->with($notification);
After adding redirect notification to your controller, add following javascript at the bottom of your layout file (app.blade.php or master.blade.php – depends on how you name it).
#if(Session::has('message'))
var type = "{{ Session::get('alert-type', 'info') }}";
switch(type){
case 'info':
toastr.info("{{ Session::get('message') }}");
break;
case 'warning':
toastr.warning("{{ Session::get('message') }}");
break;
case 'success':
toastr.success("{{ Session::get('message') }}");
break;
case 'error':
toastr.error("{{ Session::get('message') }}");
break;
}
#endif
Note* Please open and close script tag before and after the above code snippet respectively.
That’s it. Now if you visit or perform the action you have targeted, you will be shown a nice notification using toastr. You can set the alert type from info, warning, success or error and correct colored notification will be shown.
I am creating a all-in-one page for all errors in Laravl app. The error page should include a layout for currently logged in user: normal user, admin and guest. To be able to use Auth::check(), I created error page using fallback route.
Route::fallback(function () {
return view('errors.general', ['msg'=>'Error Description']);
});
And the view:
#php
if (Auth::guard('admin')->check())
$layout = "layouts.admin";
elseif (Auth::check())
$layout = "layouts.app";
else
$layout = "layouts.start";
#endphp
#extends($layout)
#section('content')
<div class="error-container">
<div class="error-box">
<div class="error-text">
{{ $msg }}
</div>
</div>
</div>
#endsection
The structure is working for 404 errors. But as for 500 errors, Laravel is showing default 500 page. How to direct all errors to use the same view with additonal error messages.
Instead of creating fallback route, handle all exception inside a Error Handler : app/Exceptions/Handler.php
And you can also check which user is logged in inside handler with below code it is just a basic example with which you can handle 500 errors like below code:
public function render($request, Exception $exception)
{
// 404 page when a model is not found
if ($exception instanceof ModelNotFoundException) {
if (Auth::guard('admin')->check()) {
// Your custom view for admin
} else {
//Your custom view for another user
}
}
if ($exception instanceof \ErrorException) {
if (Auth::guard('admin')->check()) {
// Your custom view for admin
} else {
//Your custom view for another user
}
}
return parent::render($request, $exception);
}
The fallback route is only catches non-existent routes (i.e., 404):
https://laravel.com/docs/6.x/routing#fallback-routes
But you can define your own error pages.
https://laravel.com/docs/6.x/errors#custom-http-error-pages
Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 404 HTTP status codes, create a resources/views/errors/404.blade.php.
In order to use your single error layout, you could do something like this:
resources/views/errors/500.blade.php
#include('layouts.error',['msg' => $exception->getMessage()])
EDIT: However, I do not believe that error pages load the session middleware, so you will likely have trouble attempting to use the Auth facade without additional effort.
Im trying to redirect users with "TokenMismatchException" to login page. All my website is based on JQuery, the user never change the main page, only the content, so the problem is that the response from Handler.php about the error will be passed to the jquery functions, but there's tons of functions, i dont want to make an if in all them.
I tried creating a blade with this content:
<script>
window.location.replace('https://brasilbitcoin.com.br/entrar');
</script>
And return this blade if error, but doesn't work either...
Any suggestions?
inside your Exceptions/Handler.php
public function render($request, Exception $exception)
{
if ($this->isTokenMismatchException($exception)){
return redirect('/login');
}
}
I am new in Aurelia and I want to see the result of validation. I have the following code:
public submit() {
this.controller.validate()
.then((result) => {
if (result.valid) {
// validation succeeded
} else {
// validation failed
}
});
}
How to see the result's report. How to use alert() for this purpose? For example
alert(result.getAlertText())
does not work!!!! What is the alternative?
See: source here
So that would be:
alert(result.message)
I solved it in HTML side, I added the following code in HTML file:
<ul if.bind="controller.errors">
<li repeat.for="error of controller.errors">
${error.message}
</li>
Now, I can see the type of error.
I'm having trouble getting a View Composer to fire based on a subview. Specifcally:
protected $layout = 'layouts.main';
public function index()
{
return $this->layout->content = View::make('pages.dashboard');
}
pages/dashboard.blade.php contains:
#extends('layouts.main')
#section('content')
Hello World
#stop
Inside layouts/main.blade.php contains:
<html>
<body>
#include('layouts.partials.header')
#include('layouts.partials.sidebar')
#include('layouts.partials.content') // Renders {{ $content }}
</body>
</html>
My view controller is:
View::composer('layouts.auth', function($view)
{
die("Hit the sweet spot!");
});
It will work if I use "pages.dashboard", but not "layouts.main" or "layouts.partials.sidebar". Any idea how to hook to those views?
Using the tip at Get location of view passed into laravel view composer for getting a view name, I debugged every view that was called:
View::composer('*', function($view) {
print $view->getName() . "<br>";
});
I found that the proper name to listen for is "layouts.partials.sidebar". Plugged it in and it worked! Guess it was just a typo on my part the first time.
You registered the composer for layouts.auth view
View::composer('layouts.auth', function($view)
{
die("Hit the sweet spot!");
});
So, it'll work only when layouts.auth will be rendered. To hook the composer for layouts.main you should register it for layouts.main using:
View::composer('layouts.main', function($view)
{
die("Hit the sweet spot!");
});
You may also check this article, could be helpful.