2
votes

i have a problem with routes, i have my route:

Route::get('dashboard/password', 'UserController@password');

Route::post('dashboard/updatepassword', 'UserController@updatePassword');

// PAGINA UTENTE  PUBBLICA

Route::get('/{username}', 'FrontController@user');


// blog routes

    Route::get('blog', 'FrontController@blog');
    Route::get('blog/{slug}', 'FrontController@article');
    Route::get('blog/category/{name}', 'FrontController@BlogCategory');
    Route::get('blog/tag/{name}', 'FrontController@tags');

    Route::resource('comment', 'CommentController');

and my FrontController:

public function blog()
    {

        $articles = Article::OrderBy('id','DESC')->paginate(3);
        $Allarticles = Article::OrderBy('id','DESC')->get();
        $Allcategories = BlogCategory::OrderBy('id','DESC')->get();
        $Alltags = Tag::OrderBy('id','DESC')->get();
        $Allcomments = Comment::OrderBy('id','DESC')->take(3)->get();

        return view('blog', compact('articles','Alltags','Allarticles','Allcategories','Allcomments'));
    }

if i go to "http://localhost:8000/blog" it return to page where i was before. similar to route->back().

I dont know why i have this problem, other blog routes work well.

I done some test like this:

public function blog()
        {
            return "Hi";

        }

it doesnt return "Hi", so i think is a problem with the route. i have not anable middleware here, my other routes like blog/article work well.

2
What is the full path of your blog.blade.php ? Do you maybe override the Route with another Route? Try to put your route on the very top of your routes.php - bobbybackblech
views/blog.blade.php this is the full path, i have not use a folder, i dont know why but doesnt work. If i going to a route that doesnt exist i have the same problem. - Diego Cespedes
the problem was : Route::get('/{username}', 'FrontController@user'); i deleted and it work - Diego Cespedes

2 Answers

1
votes

Could you post the contents of your routes file? If there's any routes for 'blog' above the one you've posted which contain a parameter (eg. Route::get('blog/{blog_post_id}, ...), try moving them below 'blog' in the file.

If it isn't the above then it sounds like there might be some caching at play that's screwing around with stuff, it routinely catches me out when i'm running my optimisations to see how the production environment will perform and i forget to clear all the caches, Here's my usual fixes (that i've got aliased because i mess this up so often);

php artisan route:clear
php artisan view:clear
php artisan cache:clear (Side note, clears all auth sessions, will require a re-log)
composer dump-autoload
php artisan optimize --force

This will completely clear any caches that have been created for routes, views and authorisation.

Also check your Laravel logs and your Apache/NginX logs as well, always worth having a look in those

0
votes

Your issue is pattern matching in the routes file. It seems that Routes are assigned to the first route that matches the URI.

Route::get('/{username}', 'FrontController@user');
Route::get('blog', 'FrontController@blog');

http://localhost:8000/blog matches both these routes because the {username} could be blog and thus Route::get('/{username}', 'FrontController@user'); will always be used.

You must either be more specific in the route name (e.g. add more text) or more specific in the order of the routes. Here is an example with your current routes ordered in the way you would want.

Route::get('dashboard/password', 'UserController@password');

Route::post('dashboard/updatepassword', 'UserController@updatePassword');

// blog routes

    Route::get('blog', 'FrontController@blog');
    Route::get('blog/{slug}', 'FrontController@article');
    Route::get('blog/category/{name}', 'FrontController@BlogCategory');
    Route::get('blog/tag/{name}', 'FrontController@tags');

    Route::resource('comment', 'CommentController');

// PAGINA UTENTE  PUBBLICA

Route::get('{username}', 'FrontController@user');