I can't figure out why it isnt working, i have a page to manage user role, and the only way that i can access my user role page is through browser. I mean.. if i do a view route like that, it just dont work. (Only for role.user). role.index and role.create are working just fine.
{{route('role.user')}}
I've read about resources and such.. I just dont get it. How can i solve that error. And is there a better way to do it?
my app.blade.php for extends:
<div class="container">
<nav class="navbar navbar-light">
<a class="navbar-brand" href="{{route('role.index')}}">Permissão</a>
<a class="navbar-brand" href="{{route('role.create')}}">Criar Permissão</a>
<a class="navbar-brand" href="{{route('role.user')}}">Editar Usuário</a>
</nav>
#yield("content")
and i defined it in my web.php route as resource:
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/admin', ['as'=>'admin', 'middleware'=>'role:administrador','uses'=> function(){
return view ('admin.index');
}
]);
Route::resource ('role', 'RoleController');
Route::resource ('user', 'UserController');
My UserController:
namespace App\Http\Controllers;
use App\User;
use App\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
public function index()
{
$users = User::all();
$allRoles = Role::all();
return view('admin.role.user', compact(['users','allRoles']));
}
error:
"Route [role.user] not defined. (View: C:\xampp\htdocs\cms\resources\views\adminLayout\app.blade.php) (View: C:\xampp\htdocs\cms\resources\views\adminLayout\app.blade.php)"
In your code the route is user.index; it is not role.user. If you want to create role.user you have to define it first. The resource controller does not automatically define role.user.
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code, please visit docs. you used custom Route instead "CRUD" routes and it is wrong!
Related
already try different way, still not working with different error
anyone can help?
I just learn how to use laravel route and controller,
According to the Laravel routing documentation,
You have to pass the route action as an array:
Route::get('/', [ControllerRifan::class, 'index']);
You should make route like this
Route::get('/home', [HomeController::class, 'index']);
//Step 1
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
//
public function index(){
dd('stop');
}
}
// Step 2
<?php
use Illuminate\Support\Facades\Route;
// import controller here
use App\Http\Controllers\HomeController;
Route::get('/', [HomeController::class, 'index']);
Follow the instruction step by step it will work !!
enter image description here
Hi everyone I need a solution with laravel project when I try open backend admin with /control its giving me an error "Not Found - The requested resource /control was not found on this server."
When I change the name "/control" to anything like "/control5" or something its working fine but the problem is I use /control at views and other! I am new to laravel I didn't know what the problem was? please help me out with this!
Web.php
Auth::routes();
Route::get('/about', [App\Http\Controllers\AboutController::class,'about']);
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/', [App\Http\Controllers\HomePageController::class,'index']);
Route::get('/listing', [App\Http\Controllers\ListingPageController::class,'index']);
Route::get('/details', [App\Http\Controllers\DetailsPageController::class,'index']);
Route::group(['prefix' => 'control','middleware' => 'auth'],function(){
Route::get('/', [App\Http\Controllers\Control\DashboardController::class,'index'])->name('control');
//Pages
Route::get('/pages', [App\Http\Controllers\Control\PagesController::class,'index']);
Route::get('/pages/add', [App\Http\Controllers\Control\PagesController::class,'create']);
Route::get('/pages/edit', [App\Http\Controllers\Control\PagesController::class,'edit']);
});
DashboardController
namespace App\Http\Controllers\Control;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index(){
return view('control.dashboard');
}
}
Because, you have a folder named control on /public folder. That error occurs when you create a folder in the public folder with the same name as your route so please change the name of the folder you have put in the public folder so that it has a different name from your route this will probably solve your error
Trying to use invokable controllers, but it seems to fail to find the __invoke method?
Invalid route action: [App\Http\Controllers\App\Http\Controllers\MainController].
It seems to be returning true on:
if (! method_exists($action, '__invoke')) {
throw new UnexpectedValueException("Invalid route action: [{$action}].");
}
Routes:
<?php
Route::get('/', \App\Http\Controllers\MainController::class);
MainController:
<?php
namespace App\Http\Controllers;
class MainController extends Controller
{
public function __invoke()
{
dd('main');
}
}
Laravel by default assumes that your controllers will be located at App\Http\Controllers\. So when you're adding the full path to your controller, Laravel will check it there, at App\Http\Controllers\App\Http\Controllers\MainController.
To solve it simply remove the namespace when you're registering the route, and register it like this:
Route::get('/', MainController::class);
Alternatively, you can stop this behavior by removing ->namespace($this->namespace) from mapWebRoutes() method on RouteServiceProvider class, which is located at App\Providers folder. Then you can register your routes like this:
Route::get('/', \App\Http\Controllers\MainController::class);
Alternatively, you can use:
use App\Http\Controllers\MainController;
Route::get('/', [MainController::class, '__invoke']);
In this case, the namespace provided in RouteServiceProvider won't be taken into account.
The advantage of this is that now your IDE will be able to reference the class usage and you can navigate by clicking on it.
The best answer that works for everyone is laravel documentation.
just use this at the top of your route(web.php) if (websiteController is the name of your controller)
use App\Http\Controllers\WebsiteController;
and define your route like this for your index page
Route::get('/', [WebsiteController::class, 'index']);
take note of the
[ ]
You have to create the crontoller with argument "--invokable"
php artisan make:controller YourController --invokable
Always Declare/Use in Top
use App\Http\Controllers\backend\DashboardController;
Then Use this way
Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('dashboard');
If have Auth and Use Group
Route::middleware([
'auth:sanctum',
config('jetstream.auth_session'),
'verified'
])->group(function () {
Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('dashboard');
})
Change your route to:
Route::get('/', "MainController");
In my case I forget to set #action, so change your code from:
Route::get('admin/orders', 'Admin\OrderController')->name('admin.orders');
to:
Route::get('admin/orders', 'Admin\OrderController#index')->name('admin.orders');
you have mentioned get link, but you have not declared which method it should call.
Route::get('/', \App\Http\Controllers\MainController::class);// if you are importing lass like this you have to use resource instead of get.
you can solve this issue by two ways,
first way,
Route::get('/', '\App\Http\Controllers\MainController#index'); // you have to mention your method which you have mentioned in controller
another way is,
Route::resource('/', \App\Http\Controllers\MainController::class);
In, 2nd method laravel will automatically find which request and where should redirect.2nd option is prefered if you are using multiple method for the same route.
Route::resource('/', \App\Http\Controllers\MainController::class);
Use method 'resource'
Possible duplicate of this. But unable to get the answer in my case.
I need to use a link on my Laravel website, but it keeps resulting in "route not defined" error.
Html:
<ul class="nav navbar-nav">
#auth
<li>
Add post
</li>
#endauth
</ul>
web.php:
Route::get('/add-post', 'PagesController#add_post');
PagesController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function add_post()
{
return view('add-post');
}
}
You need to name the route in order to do that:
For example:
Route::get('/add-post', 'PagesController#add_post')->name('add-post);
This is because when you use route('add-post') are are requesting a URL based on the name set in the web.php file
So you basically have two options here. When you use the route function, Laravel is looking for a named route. In order to name a route, you can add ->name('name-of-route-here') at the end of your route definition.
If you don't want to name your route, you can just use the url helper function instead of route, so your code would be url('add-post')
Documentation on named routes: https://laravel.com/docs/5.5/routing#named-routes
Documentation on url function: https://laravel.com/docs/5.5/helpers#method-url
Just change this:
<ul class="nav navbar-nav">
#auth
<li>
Add post
</li>
#endauth
web.php:
Route::get('add-post', 'PagesController#add_post');
I cloned this todstoychev/Laravel5Starter from Github and installed it.
After creating this StaticPagesController controller and updating my routes.php file. The controller does not seem to work. For some reason i keep getting the following error.
ReflectionException in ControllerInspector.php line 32:
Class App\Http\Controllers\StaticPagesController#faq does not exist
My routes.php file
<?php
// Admin routes
Route::group(['prefix' => 'admin', 'namespace' => 'Admin'], function () {
Route::controller('permissions', 'AdminPermissionsController');
Route::controller('settings', 'AdminSettingsController');
Route::controller('roles', 'AdminRolesController');
Route::controller('users', 'AdminUsersController');
Route::controller('/', 'AdminController');
});
// Public and user routes
Route::controller('contacts', 'ContactsController');
Route::controller('users', 'UsersController');
Route::controller('/', 'IndexController');
Route::controller('faq', 'StaticPagesController#faq');
My StaticPagesController.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class StaticPagesController extends Controller
{
public function faq(){
return 'this is faq page';
}
}
I have tried composer update, php artisan acl:update, composer dumpautoload to no avail.
Please help me. Thanks
With this line:
Route::controller('faq', 'StaticPagesController#faq');
You are telling Laravel that the controller for faq shoule be StaticPagesController#faq. The Route::controller method sets an entire controller for a route, it does not specify a method to be used on that route, Laravel handles this internally. Take a look at your error to prove my point:
Class App\Http\Controllers\StaticPagesController#faq does not exist
It is looking for class StaticPagesController#faq not StaticPagesController as you are intending.
Unless you are building an API using REST, you should not use the controller method and instead specify your routes explicitly, i.e.
Route::get('faq', 'StaticPagesController#faq');
This will use the faq method on your controller when the user makes a GET request to the URI faq. If you insist on using the controller method, then remove the #faq from the second argument and you will be good, although I'm pretty sure Laravel expects the methods index, show, create, etc to be in your controller. I suggest taking a look at the Laravel 5 Fundamentals video course to help you get a better understanding.