简体   繁体   中英

Error Messages - Laravel 5.2

Can someone please tell me how to make my "Page not found" or something messages? For example if someone writes a link in the browser which do not exist in my project, not to show the standard error page ( Woops, something went wrong, View [bla.bla] not found ) but page specified by me?

<?php



Route::get('sendemail', 'EmailController@sendEmail');

Route::get('test', 'AuthController@getTest');

Route::get('napravisiadmin', 'ClassbookController@getIndex');
Route::group(['middleware' => ['web']], function () {

    Route::group(['middleware' => ['guest']
    ], function () {
        Route::get('login', 'AuthController@getLogin');
        Route::post('login', 'AuthController@postLogin');
    });
    Route::get('logout', 'AuthController@getLogout');


    //Admin
    Route::group(['middleware' => ['auth', 'auth.admin']
    ], function () {
        Route::group([
            'prefix' => 'admin',
            'namespace' => 'Admin'
        ], function () {
            Route::controller('student', 'StudentsController');
            Route::controller('profile', 'ProfilesController');
            Route::controller('class', 'ClassesController');
            Route::controller('subjects', 'SubjectsController');
            Route::controller('teacher', 'TeachersController');
            Route::controller('marktype', 'MarkTypeController');
            Route::controller('rules', 'RuleController');
            Route::get('{slug?}', 'PageController@getView');
        });
    });
    //Admin


    //Student
    Route::group([
        'middleware' => ['auth', 'auth.student'],
        'prefix' => 'stu',
        'namespace' => 'Stu'
    ], function () {
        Route::get('{slug?}', 'StuController@getView');
    });
    //Student


    //Teacher
    Route::group([
        'middleware' => ['auth', 'auth.teacher'],
        'prefix' => 'educator',
        'namespace' => 'Educator'
    ], function () {

        Route::get('edit/{id}', 'AccountController@getEdit');
        Route::post('edit/{id}', 'AccountController@saveEdit');
        Route::get('account', 'AccountController@getView');
        Route::get('class-subject', 'AccountController@getClassSubject');
        Route::get('add-mark', 'AccountController@getAddMark');
        Route::post('mark', 'AccountController@postAddMark');
        Route::get('added', 'AccountController@marksList');
        Route::get('statistics', 'AccountController@marksInTable');
        Route::get('personalemails', 'PersonalEmailController@getView');
        Route::post('personalemails', 'PersonalEmailController@personalEmail');

    });
    //Teacher
});



Route::get('{slug?}', 'PageController@getView');

For the "Page not found" 404 error create a view in resources/views/errors/404.blade.php and it will show when you get a 404 error.

From the documentation:

Custom HTTP Error Pages

Laravel makes it easy to return 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. This file will be served on all 404 errors generated by your application.

The views within this directory should be named to match the HTTP status code they correspond to.

https://laravel.com/docs/5.2/errors#custom-http-error-pages

You can always go a step further by utilising the exception handler and handling exceptions the way you desire by customising the render() method

https://laravel.com/docs/5.2/errors#the-exception-handler

For example, if you wanted to handle file not found error, Exceptions\\Handler.php

public function render($request, Exception $e)
{

    if ($e instanceof \Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException) {
        return response()->view('errors/exceptions/file-not-found', [], 500);
    }

    return parent::render($request, $e);
}

You can create custom error 404 page . If someone will enter wrong URL in a browser, he will see that page.

Also, you can redirect user manually to this page with:

abort(404);

Update

I guess the problem is here:

Route::get('{slug?}', 'PageController@getView');

You're using this three times, try to remove all of them.

The thing is when Laravel doesn't find any routes, it takes {slug} and passes it to the PageController , so when you enter http://example.com/sometext , you will be transferred to the PageController with slug = sometext .

If you do not want to remove it, check for slug inside a controller and if slug means something - good. If not, just abort(404); and user will be transferred to an error page.

Also, if you're on 5.2.27 of higher, remove web middleware from routes.php (it applies automatically, and manual apply can cause errors and strage behavior).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM