Laravel 5.1 - Route doesnt work - php

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.

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

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');

Related

Out of a sudden my /admin route inside the Auth Middleware in Laravel 8 returns a 404 not found

I recently started to code my own Admin panel in Laravel.
Every route was working fine, but all of a sudden the /admin route inside the Auth middleware group stopped working properly.
This are my routes inside web.php
My php artisan route:list
And the EntryController#index looks like this:
public function index()
{
//
$entries = Entry::all();
return view('admin.index', ['entries' => $entries]);
}
I'm having this problem for about 2 now, so maybe one of you know the solution.
I think you're having this issue because of how Laravel prioritises its routes.
And I think the culprit might be this route:
Route::get('/{link}', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
When you use {link} you are basically saying: "expect anything in this segment of the URI". Since the /{link} route is placed before the /admin route, and their URIs both contain only one segment, Laravel will try to resolve /{link} first.
Solution: just move the /{link} route below the /admin route. Might be best to just place it at the bottom of the list :D

Laravel throwing 404 for an existing route

Can someone help me understand what is wrong with these routes:-
From the list of these routes, the third and the last one returns 404. There is no issue with the controllers. They show up as expected when I run php artisan route:list.
Route::get('/uploads', 'ImageController#adminIndex')->name('admin.images.index');
Route::get('/uploads/{image}', 'ImageController#adminShow')->name('admin.image.indivisual');
Route::get('/uploads/request', 'ImageController#imageRequests')->name('admin.images.request');
Route::get('/uploads/request/{image}', 'ImageController#individualRequest')->name('admin.images.request.individual');
Route::post('/uploads/accept', 'ImageController#acceptImage')->name('admin.accept.image');
Route::post('/uploads/decline/', 'ImageController#declineImage')->name('admin.decline.image');
Route::get('/uploads/all', 'ImageController#index')->name('admin.images.list');
The thing that confuses me is that changing uploads to images for these two routes solved the problem and they work just fine.
Route::get('/uploads', 'ImageController#adminIndex')->name('admin.images.index');
Route::get('/uploads/{image}', 'ImageController#adminShow')->name('admin.image.indivisual');
Route::get('/images/request', 'ImageController#imageRequests')->name('admin.images.request');
Route::get('/uploads/request/{image}', 'ImageController#individualRequest')->name('admin.images.request.individual');
Route::post('/uploads/accept', 'ImageController#acceptImage')->name('admin.accept.image');
Route::post('/uploads/decline/', 'ImageController#declineImage')->name('admin.decline.image');
Route::get('/images/all', 'ImageController#index')->name('admin.images.list');
I have tried php artisan route:clear.
Also, there are no folders in the public directory to create any conflicts.
Note: All the routes are grouped in
Route::group(['prefix' => 'admin', 'middleware' => 'role:administrator|auth'], function () {
// Other routes in this group are working just fine. No issues.
});
Appreciate the help.
Please move the router into last of list:
Route::get('/uploads/{image}', 'ImageController#adminShow')->name('admin.image.indivisual');
Because It includes Route::get('/uploads/request' and Route::get('/uploads/all' then It override this two routers
So code of routers list:
Route::get('/uploads', 'ImageController#adminIndex')->name('admin.images.index');
Route::get('/uploads/request', 'ImageController#imageRequests')->name('admin.images.request');
Route::get('/uploads/request/{image}', 'ImageController#individualRequest')->name('admin.images.request.individual');
Route::post('/uploads/accept', 'ImageController#acceptImage')->name('admin.accept.image');
Route::post('/uploads/decline/', 'ImageController#declineImage')->name('admin.decline.image');
Route::get('/uploads/all', 'ImageController#index')->name('admin.images.list');
// move to last
Route::get('/uploads/{image}', 'ImageController#adminShow')->name('admin.image.indivisual');

Laravel 5.6 404 event

I've been going round in circles on this.
We wrote a CMS system in Laravel 3 using a bundle.
Time has come to develop a new one using latest Lavarel 5.6 and that mean packages.
We want to be able to define our own routes in web.php but everything that is not defined is picked up by the CMS package routes file so it can check if there is a page defined in the CMS and return the correct view.
In L3 this we did:
Event::override('404', function() {
...magic
In laravel 5.6 you can't do this so i've tried all sorts of:
Route::any('/{any}', function ($url = false) {
})->where('any', '.*');
But the issue is Laravel loads all the routes files in memory and the /{any} route overrides any of the routes defined in web.php, regardless of the order the service providers are loaded and we want to allow routes to be defined but to mop up anything that is not already defined.
In L4 it looks like you used to be able to do this:
App::missing(function($e) {
But again that's not possible in L5
I could possibly run it though an exception handler, but I want this to work in the package so its easily installable, and I haven't been able to make this work either!
Any help would be appreciated.
// other routes redirected to login
Route::get('/{any}', function () {
return redirect('/login');
})->where('any', '.*s');
try this route

Delete session in laravel5.2 when cms pages accessed

I am new in laravel framework. Can anyone tell me how to delete sessions when cms pages accessed i.e(faq,privacy policy,about us). This query runs fine for me:-
$request->session()->forget('key');
The problem is that when i accessed the faq page i have write this query and when i accessed the privacy policy then again i have to write this query. Can anyone tell me how i do in one function. So i have not implement this query again and again
Thanks in advance :)
Create One middleware named as forgetSession(you can have any name) and set the cms pages route group in app\Http\routes.php under that middleware for eg.
Route::group(['middleware' => ['forgetSession']], function () {
Route::resource('faq', 'faqController');
Route::resource('privacy', 'privacyController');//likewise
});
Now create middleware by writing below command on cmd project root
php artisan make:middleware forgetSession
So it will create the middleware in app/Http/middleware/forrgetSession
and put your code
$request->session()->forget('key');
So in this way all route mentioned under route group will have the code to forget session. This way definitely you can redundant the code.
you can remove session at your controller
call this in controller contruct method
$request->session()->forget('key'); //remove by key
$request->session()->flush(); // remove all

Laravel 5.1 routing returning wrong content from controller

I have this really weird problem with laravels routing.
I started to make some routes and controllers and just returning strings from each controller confirm that it worked.
And everything did work.
Now when i started making the master view and putting it together with some templates for the routes I noticed that the string that laravel returns isn't the string i wrote.
All routes return "This is routename page"
The only routes that actually work as expected is the routes with wild cards, and the route going to the start page.
Those routes return the correct strings.
Example routing
Route::get('/users', 'UserController#index');
class UserController extends BaseController {
public function index() {
return 'List of users!';
}
});
This routing displays "This is user page" (NO ERROR)
I have tried returning the string directly from the route, clearing all the cache files i could find including route cache, restarting browser and MAMP
Just to be clear, the routing returned the correct strings when I made the route.
I have installed Elixir to compile my scss files, but i doubt that should have anything to do with my problem.. :(
Figured it out just after I posted the question!
I had a route with a wildcard directly after the root
Route::get('/{'user'});
This route were overriding all other routes that only had one parameter after the root. So if I go to the url "/users" the route will assume it is a wildcard and send it to another controller that returns the string "This is {wildcard} page!", Brainfreez! :P

Categories