Laravel error: route not defined - php

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

Related

Laravel routing not working correctly for static page

/resources/views/layout/partials/nav.blade.php
<li>About</li>
/routes/web.php
Route::resource('tasks', 'TaskController');
/app\Http/Controllers/TaskController.php
public function about()
{
return view('tasks.about');
}
/resources/views/tasks/about.blade.php
#extends('layout.layout')
#section('content')
<div><h3>hello world </h3></div>
#endsection
Getting this error
Sorry, the page you are looking for could not be found.
change url to route and defined it into route file.
<li>About</li>
Route.php
Route::get('task/about', 'TaskController#about')->name('task.about');
If you want to use direct URL then you may use {{ url('task/about')}} instead of {{ route('task.about') }}
Note: Because as you defined resource in web.php file it contains many methods and URL.
Route::resource('tasks','eventcontroller');
Verb Path Action Route Name
GET /tasks index tasks.index
GET /tasks/create create tasks.create
POST /tasks store tasks.store
GET /tasks/{tasks} show tasks.show
GET /tasks/{tasks}/edit edit tasks.edit
PUT|PATCH /tasks/{tasks} update tasks.update
DELETE /tasks/{tasks} destroy tasks.destroy

Class 'auth' not found in some views

i put this code to my header layout that get a field from users table
<li class="tophe">
Your ID is : {{auth::user()->shenase}}
</li>
This code works good to my index page but when i open other pages i get this error
Class 'auth' not found (View:
###\resources\views\layouts\panel\header.blade.php)
What's the problem?
In laravel, there is an auth helper you can use to get the information you need about the currently authenticated user and you use it like so :
<li class="tophe">
Your ID is : {{auth()->user()->someAttribute}}
</li>
// not auth alone it's a function
You should use it as Auth, or auth():
<li class="tophe">
Your ID is : {{auth()->user()->shenase}}
</li>
Or:
<li class="tophe">
Your ID is : {{Auth::user()->shenase}}
</li>
Hope it helps.
<li class="tophe">
Your ID is : {{\Illuminate\Support\Facades\Auth::user()->shenase}}
</li>
You can try with give absolute path to auth class

Laravel 5.7 Route [role.user] not defined

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!

Missing required parameters for [Route: homepage.edit] with link in layout

I'm new to Laravel 5.4 and php and I'm having some trouble to do my routing, so I wish you would explain to me what's going on !
I'm developing a back office for my future projects, and for my current project I need to be able to update data sent to the homepage. The edit of the page and the update of the data is now fully functional, I'm just having some trouble to do the link in the layout. Pretty sure it's a trivial problem, yet i'm having trouble figuring it out.
Here is my code :
web.php :
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function()
{
Route::resource('homepage', 'Admin\HomeController');
});
HomeController :`
public function edit($id) {
$contentExists = Homepage::first();
$homepage = Homepage::findOrFail($id);
return view('admin/homepage/edit', compact('homepage', 'contentExists'));
}`
In the layout :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
Now for that I know I need to pass a parameter to the route, so I did like so :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
But then it says the homepage variable is not defined, and I have no idea where I can define this variable since I'm in the layout, and also have no idea what needs to be inside this variable, what's his purpose... Can you help me with that ?
route second parameter, must be an array like this for example:
route('homepage.edit', ['id' => 2])

404 not found laravel

I am new to Laravel , I am getting 404 not found error when returning view to salary report from my controller. The below mentioned is my function which returns my simple view to salary report.
public function getSalaryReport()
{
return view('Company.salaryReport');
}
the routes.php file ahs route to company controller.
Route::group(['middleware' => 'auth.company'], function () {
Route::get('company/notice-board/create', 'CompanyController#getNoticeBoardCreate');
Route::get('company/notice-board/{id}/edit', 'CompanyController#getNoticeBoardEdit');
Route::get('company/designation/{id}/edit', 'CompanyController#getDesignationEdit');
Route::get('company/all-user/{id}/force', 'CompanyController#getForce');
Route::post('company/all-user/{id}/force', 'CompanyController#postForce');
Route::controller('company', 'CompanyController')
this is my view which i am trying to display from my controller.
#extends('Company.CompanyLayout')
#section('content')
<div>
<ul class="breadcrumb">
<li>
Home <span class="divider">/</span>
</li>
<li>
<a href='{!! URL::to("company/report-summery") !!}'>Summery Report</a>
</li>
</ul>
</div>
#endsection
where i am going wrong and what should be done to make my view visible. Thanks to all in advance.
Route::controller is depricated in the latest versions of Laravel, try not to use it anymore.
You can use Route::resource or create a specific route for your salary report like this:
Route::get('company/salary-report', 'CompanyController#getSalaryReport');
Also make sure that you have resources\views\Company\salaryReport.blade.php as your view.
404 not found is an error because you don't have any routes for the given url. And I didn't find any routes in your example for the function getSalaryReport()
if you want to call this method, at least add this to your routes:
Route::get('company/report-summery', 'CompanyController#getSalaryReport');

Categories