-1

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');
Yoanna Murdzheva
  • 249
  • 1
  • 4
  • 15
  • 1
    check [here](http://stackoverflow.com/questions/34250180/laravel-5-how-to-handle-error-messages/34250468#34250468) for more info on how to deal with errors – Moppo May 04 '16 at 12:59

2 Answers2

3

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);
}
haakym
  • 12,050
  • 12
  • 70
  • 98
1

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).

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • I'm creating that view, but when i enter wrong url still do not see the view i created. :( – Yoanna Murdzheva May 04 '16 at 11:01
  • Did you add `resources/views/errors/404.blade.php` view? If you did, what exactly URL you're trying to enter and what exactly do you see? – Alexey Mezenin May 04 '16 at 11:04
  • yep, i did add it exactly there with that name. – Yoanna Murdzheva May 04 '16 at 11:08
  • and getting InvalidArgumentException in compiled.php line 14014: View [pages.somerandomtext] not found. – Yoanna Murdzheva May 04 '16 at 11:09
  • Glad I could help. ) – Alexey Mezenin May 04 '16 at 11:28
  • Hi @AlexeyMezenin I saw in one of your other answers yesterday you mentioned about the `web` middleware is applied automatically in `5.2.27`. Where did you read this info as I didn't see it in the release notes here https://laravel.com/docs/5.2/releases#laravel-5.2, maybe it's on the git pages? Please provide a link if possible as it would be good to keep up with changes. Many thanks. – haakym May 04 '16 at 11:31
  • 1
    @haakym, I've answered you here (udated my answer with code): http://stackoverflow.com/questions/36784253/laravel-5-2-validation-error-not-appearing-in-blade - you can download both 5.2.27 and 5.2.24 versions and compare `RouteServiceProvider.php` and `routes.php` files in both archives. – Alexey Mezenin May 04 '16 at 11:49
  • Oh okay - so you just looked directly at the code then. I thought perhaps you were following a change log or something. Thanks. – haakym May 04 '16 at 12:53
  • FYI, you can keep track of changes here using Mike Bronner's excellent site: https://laraver.xyz/home – haakym May 09 '16 at 08:56