How to Custom 404 Page Not Found in CodeIgniter 4 - php

I just learn CodeIgniter 4 Framework. How to customize my 404 page not found?

Go to your Routes.php file and find
$routes->set404Override();
Now , If you want to display just an error message then write
$routes->set404Override(function(){
echo "your error message";
});
instead of
$routes->set404Override();
Or
If you want to return a view then write like below:
$routes->set404Override(function(){
return view('your_filename');
});
instead of
$routes->set404Override();

// Would execute the show404 method of the App\Errors class
$routes->set404Override('App\Errors::show404');
// Will display a custom view
$routes->set404Override(function()
{
echo view('my_errors/not_found.html');
});

To customize your 404 page, modify app/Views/errors/html/error_404.php to your liking.

For custom error messages
in Config\routes.php
// Custom 404 view with error message.
$routes->set404Override(function( $message = null )
{
$data = [
'title' => '404 - Page not found',
'message' => $message,
];
echo view('my404/viewfile', $data);
});
in my viewfile, to display error:
<?php if (! empty($message) && $message !== '(null)') : ?>
<?= esc($message) ?>
<?php else : ?>
Sorry! Cannot seem to find the page you were looking for.
<?php endif ?>
from my controller:
throw new \CodeIgniter\Exceptions\PageNotFoundException('This is my custom error message');

Related

Yii2 setFlash not work or lost if redirect

I want to set flash message, but flash message always missing. the flow of my controller like this:
public function actionOne()
{
Yii::$app->session->setFlash('error', 'message text');
return $this->redirect(['two', 'param1' => 'value1']);
}
public function actionTwo()
{
return $this->render('view');
}
And on the file view.php
<?php
$flashes = Yii::$app->session->getAllFlashes();
var_dump($flashes);
?>
I always get empty. How I can show flash message after redirect?
In case when you redirect to another action - You have to pass third parameter false. Link to documentation: setFlash()
Yii::$app->session->setFlash('error', 'message text', false);
In other cases, when you want to show the message in the same action you can use it like this:
Yii::$app->session->setFlash('error', 'message text'); // default third parameter is set to true.

Laravel Authenticate middleware flash message

In my auth middleware I have the following code:
if (! Auth::user()->enabled) {
Auth::logout();
// $request->session()->flash('Test', 'Test');
// return redirect()->route('site::home');
return redirect()->route('site::home')->withFlash('Test');
}
I'm trying to redirect the user back to the login page with a flash message but whenever I look into the session() there's no flash message.
What am I doing wrong? it feels like i've tried every variation of a flash message and none of it seems to work
return redirect()->route('site::home')->flash('This is a message!');
OR
return \Redirect::back()->withWarning( 'Message you want show in View' );
Try this instead
return redirect()->route('route_name')->with('custom_key', 'Custom Message');

Laravel Not getting 404 errors

Hi there I'm having a problem with getting 404 error pages. At the moment all I get is a blank page with no data in it, Below is the code that I have in 3 different pages, I have my missing.blade.php page inside a folder called errors which is in the views folder, can someone tell me if I'm missing something else
// Routes file
App::error(function(ModelNotFoundException $e) {
return Response::view('errors.missing', array(), 404);
});
App::missing(function($exception) {
return Response::view('errors.missing', array(), 404);
});
// Error 404 Page
#extends('layouts.master')
#section('page-title') #parent
Error 404
#stop
#section('content')
<h1>An error occured 404, page missing</h1>
#stop
// Controller
public function edit($id)
{
$product = Product::findOrFail($id);
}
You will need to add your error handlers to
app/start/global.php

How to detect missing parameter route and show relevant message

Working on Laravel 4, I created following route and able to fetch ID:
Route::get('details/{ID}', 'Exchange#details');
Issue is when I miss ID in route then it shows: NotFoundHttpException error. I want to avoid this. How do I proceed? Even in global.php I added following but did not work:
App::missing(function($exception)
{
print "Not Found";
// return Response::make(
// View::make('errors/404')
// , 404);
});
Controller Method
public function details($exchangeID)
{
echo "Print Details";
echo $exchangeID;
if(intval($exchangeID) == 0)
{
throw new NotFoundException;
}
}
Update: Recommendation of redirecting to 404 works but I am more interested to detect missing ID and load my Own View with proper message information instead of showing plain 404 page.
I would try something like this
# file routes.php
Route::get('{ID?}', 'SomeController#getDetails')
# file SomeController.php
function getDetails($ID=null) {
if empty($ID) {
return Redirect::to('PREVIOUS PAGE')
->withInput()
->with('error', 'invalid value');
}
}
Try like this :
App::missing(function($exception)
{
return View::make('errors.404');
});
The view is located in views/errors/404.blade.php

Codeigniter Flashdata - Not Displaying My Message

I am using this extension of the CI_Session Class. To create flash data and style them using TW Bootstrap. However when I go to pass the "Success" message to the view it's just not loading in at all.
I have had a good luck but no joy with it.
My Controller looks like this..
// Set form Validation
$this->form_validation->set_rules('title', 'Title', 'required|trim|is_unique[films.title]');
if($this->form_validation->run() == FALSE)
{
$data['page_title'] = 'Add New DVD';
$this->load->view('_partials/_header', $data);
$this->load->view('_partials/_menu');
$data['genres'] = $this->genre_model->get_genres();
$data['classification'] = $this->classification_model->get_classifications();
$this->load->view('create', $data);
$this->load->view('_partials/_footer');
}
else
{
// Insert into DB
$film_data = array
(
'title' => $this->input->post('title'),
'genre_id' => $this->input->post('genre'),
'classification_id' => $this->input->post('classification')
);
$this->film_model->create($film_data);
$this->session->set_success_flashdata('feedback', 'Success message for client to see');
redirect('dvd');
and my View is....
<?php $this->session->flashdata('feedback'); ?>
So when I insert a new item to the DB, The Model runs, The redirect runs but the "set_success_flashdata" doesnt.
The MY_session library is set to load automatically as per CI's default config.
How do I get this damn Flash Message to show ::( ?
It is not set_success_flashdata it is just set_flashdata.
Change in Controller :
$this->session->set_flashdata('feedback', 'Success message for client to see');
View is just as it is :
echo $this->session->flashdata('feedback');
Hope this helps you. Thanks!!
Although I fail to see the point of this particular extension, the answer is simple enough.
You are not echoing the data from the Session Library.
The correct way to echo flashdata is like so:
<?php echo $this->session->flashdata('feedback'); ?>

Categories