I am setting up my own admin for a Laravel project and everything is going along fine until I hit what seems to be a routing issue. Here's my situation so far.
Within my views folder I have a folder named panel which holds all of my views for the admin panel. This is working perfectly. I have full access to the panel without a problem. Within the panel directory I have a folder named users which holds my views for the UsersControllers. This is where I'm struggling. My route for those views is as follows:
Route::resource('users', 'UsersController');
Route:list shows those routes as users.index, users.store etc.
In the top nav bar of the panel I have a link to the users index as
<li>Users</li>
I've also tried using
<li>Users</li>
Either way, this should be calling the index() method of the UsersController. That method looks like this
public function index()
{
return view('panel.users.index');
}
I've also tried just
public function index()
{
return view('users.index');
}
No matter what I try I get
NotFoundHttpException in RouteCollection.php line 161:
I would really appreciate a bit of wisdom on how to resolve this one.
you can use this for index
<li>Users</li>
or you can use action
<li>Users</li>
Related
I am building my first Laravel app with the Metronic 8 Laravel theme. It uses Breeze for authentication. I changed a couple of things around - created a welcome page for non-logged-in users, and moved the main template that was the index to an auth protected "/dashboard". The problem is that it still tries to load the dashboard Blade template, regardless of authentication, resulting in an error.
Route
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
Here's Authenticate, where it should redirect non-authenticated users to the login page.
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
return route('login');
}
}
When I'm not logged in and navigate to the dashboard URL, it attempts to load the dashboard Blade template, which calls a menu function that checks the user permissions for menu items. Unfortunately, since there is no user, the application blows up from passing a null value to a method expecting a user array/object.
Any ideas on where to look for the problem? It seems to me that the auth middleware should redirect to the login page before trying to load the Blade template when not logged in.
I would put the middleware at the beginning of the route like this, though I'm sure it's not causing the problem-
Route::middleware(['auth'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Aside from that, please provide some information on the error itself like what the error is about/what is says..etc...
First of all, make sure you have a login named route defined in your routes/web.php file. It should look something like:
Route::get('/login', '<controller>#<method>')->name('login');
The important bit is ->name('login') so that the Authenticate middleware can correctly identify the route to redirect to. Change <controller>#<method> appropriately to route to the login method of your app.
Wakil's answer is irrelevant and actually opposite of the documentation. Your syntax is correct.
I figured out the issue. Keen Themes put a call to a method to build an array of menu items in the web routes file. That was making the call to the offending code. After I wrapped that in an auth check the error was fixed, and everything works as expected.
it appears that when I created a new route, I receive the 404 error when trying to access the url, which is funny,. because all of my other routes are working just fine.
My web.php looks like so:
Auth::routes();
Route::post('follow/{user}', 'FollowsController#store');
Route::get('/acasa', 'HomeController#index')->name('acasa');
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Route::get('/profil/{user}/edit', 'ProfilesController#edit')->name('editareprofil');
Route::patch('/profil/{user}', 'ProfilesController#update')->name('updateprofil');
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Route::get('/alerte/url/{user}', 'UrlsController#index')->name('editurl');
Route::post('/alerte/url/{user}', 'UrlsController#store')->name('updateurl');
Route::get('/alerte/url/{del_id}/delete','UrlsController#destroy')->name('deleteurl');
The one that is NOT working when I am visiting http://127.0.0.1:8000/alerte is:
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The controller looks like so:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class PaginaAlerte extends Controller
{
public function __construct() {
$this->middleware('auth');
}
public function index(User $user)
{
return view('alerte');
}
}
I am banging my head around as I cannot see which is the problem. It is not a live website yet, I am just developing on my Windows 10 pc using WAMP.
Moved my comment to a little bit explained answer.
So, in your route collection, you have two conflicting routes
Route::get('/{user}', 'ProfilesController#index')->name('profil');
and
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Imagine that Laravel is reading all routings from top to bottom and it stops to reading next one after the first match.
In your case, Laravel is thinking that alerte is a username and going to the ProfilesController#index controller. Then it tries to find a user with alerte username and returning 404 because for now, you don't have a user with this username.
So to fix 404 error and handle /alerte route, you just need to move the corresponding route before /{username} one.
But here is the dilemma that you got now. What if you will have a user with alerte username? In this case, the user can't see his profile page because now alerte is handling by another route.
And I'm suggesting to use a bit more friendly URL structure for your project. Like /user/{username} to handle some actions with users and still use /alerte to handle alert routes.
The following route catches the url /alerte as well
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Since this one is specified before
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The /alerte will go the the ProfilesController instead.
To fix this change the order of the url definitions or change either of the urls to have nesting e.g. /alerte/home or /user/{user}
Well.
Maybe this is too late, but I have all week dealing with this problem.
I made my own custom.php file and add it in the routes path of my Laravel project, and none of the routes were working at all.
This is how I solved it:
You must remember to edit the RouteServiceProvider.php file located in app\Providers path. In the map() function, you must add your .php file. That should work fine!
To avoid unexpected behaviors, map your custom routes first. Some Laravel based systems can "stop" processing routes if no one of the expected routes rules were satisfied. I face that problem, and was driving me crazy!
I would wish suggest to you declare your URL without the "/", like your first "post" route, because sometimes, I have been got this kind of errors (404).
So, my first recomendation is change the declaration of the route. After that, you should test your middleware, try without the construct, and try again.
Good luck!
working a project with Laravel 5.6.
the problem is, when im going to an url like:
/login
or any other route and specify where it should go, it making its own route and going to another place. no metter if i even clear the code of that blade file.
i have not several routes or blade file that are the same. i have cleared my browser cache, laravel cach, config cache, and the command:
php artisan route:cache
did not worked to clear route cache.
my code example: web.php code
Route::get("/login", "LoginController#login");
Example: LoginController.php code
public function login()
{
return view('/login'); // not going to this path
}
to conclude, it does not read my code :(
need your ideas!
public function login()
{
return view('login');
}
view accepts the view name not the path of the route, If you want go to any route use redirect("/route_name") . But in your case if you redirect to login route again it will throw exception because the login route again calls this function. so you need to pass the view name. for example:
if your login view is in
resources
- views
-login.blade.php
then use above code. or if the login page is in any other folder inside view
it will be like return view("foldername.login")
I have a website in laravel framework and I am trying to add a simple new static page to the admin panel. I have done the following three steps:
Add a template to the views:
app/views/admin/MessageToAll.blade.php
Add the make view code in the controller.
public function MessageToAll(){
return View::make('admin.MessageToAll');
}
Added a route in app/routes.php
Route::get('/admin/MessageToAll',array('as'=>'MessageToAll','uses'=>'AdminController#MessageToAll'));
But when I go to to domain.com/admin/MessageToAll
it gives me a 404 page not found error. Does anyone know what have I missed as I think I have completed all steps for adding this view.
Just put your new route before the /admin/ route (to test it, you want to temporarily make it the very first route in routes.php). The problem is /admin/ or some other similar route executed before your new route.
Also, if you need to just execute static view, you can use something like this (works without using a controller):
Route::get('/admin/MessageToAll', function (){
return View::make('admin.MessageToAll');
});
in routes add:
Route::get('/admin/MessageToAll','yourController#yourMethod');
This is my first project in Laravel, so play nice with me!
The goal is to create a CMS. Every page will have it's own "slug", so if i name a page This is a test, it's slug will be this-is-a-test. I want to be able to view that page by going to example.com/this-is-a-test.
To do that, i'm guessing i'll have to do something like:
Route::any('(:any)', 'view#index');
And create a controller called View with an index method. All good, right?
The problem is creating the admin area. I'm gonna have a few pages within the area, a few examples are Dashboard, Pages, Settings and Tools. Since all of those are sub-pages in the admin, i figured it would be appropriate to make them nested controllers, right? The only problem is, when i visit /admin, i want to show the dashboard (/admin/dashboard) directly. I would prefer just calling the dashboard controller instead of redirecting to /admin/dashboard from the admin controller. Is that possible?
So, to illustrate what i mean:
example.com/admin -> loads admin.dashboard
example.com/admin/dashboard -> also loads admin.dashboard
Here are all my routes:
Route::get('admin', array('as' => 'admin', 'use' => 'admin.dashboard#index'));
Route::get('admin/dashboard', array('as' => 'admin_dashboard', 'use' =>
Route::any('/', 'view#index'); // Also, should this be below or above the admin routes? This route will show the actual cms pages.'admin.dashboard#index'));
And here is my admin_dashboard controller:
class Admin_Dashboard_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
return 'in dashboard';
}
}
The view controller just displays a link to the admin page, that works. I just can't figure out what's wrong with the admin routes? When i go to /admin or admin/dashboard i just get a blank page, no 404. If i go to admin/blah or just blabla i get a 404, so i know something is happening, it's just not happening correctly. Am i missing something?
I had misunderstood the naming conventions for controllers.
This is how it was:
controllers
admin
admin_dashboard.php Containing controller Admin_Dashboard_Controller
It's suppose to be:
controllers
admin
dashboard.php Containing controller Admin_Dashboard_Controller
In other words, i shouldn't have prepended "admin" to the beginning of the controllers file name. It all works fine now.
I was actually able to minify the routing code to this as well:
Route::get(array('admin', 'admin/dashboard'), array('as' => 'admin', 'uses' => 'admin.dashboard#index'));