Laravel 5.3 Route in template showing Route Not Defined - php

i am updating my application from laravel 5.2 to 5.3. Most of the things seems to work fine.
But i dont know what is happening but when i am trying to define route in anchor tag, its not working. I have done something similar to this:
<a href="{{route('backend.pages.index')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>
Its showing error Route [backend.pages.index] not defined.. Here is how the created the route.
Route::group(['middleware' => ['web']], function () {
Route::resource('backend/pages','Backend\PagesController');
});
I have a template called 'mainmenu.blade.php' in which i have use this route. This mainmenu is called in main structure through #include('layouts.backend.backendstructure.mainmenu').
Is routing method is changed in laravel 5.3? Or is there any mistake from my side?
Thank you!(Advance)

The problem here is
{{route('backend.pages.index')}}
instead use
<a href="{{route('backend/pages')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>
The route is defined as backend/pages. To return view add a method in PagesController and return the view there.
Route::group(['middleware' => ['web']], function () {
Route::resource('backend/pages','Backend\PagesController#dummymethod');
});
Dummy method
public function dummymethod
{
return view('backend.pages.index');
}
Edit
I think you're looking for something like this
Route::resource('backend/pages','Backend\PagesController', ['names' => ['index' => 'backend.pages.index']]);
Check the docs here

You should write your code like this:
<a href="{{ route('backend/pages')}} " class="nav-link ">
<span class="title">All Pages</span>
</a>
or like this:
<a href="{{ url('backend/pages') }}" class="nav-link ">
<span class="title">All Pages</span>
</a>

Try:
<a href="/backend/pages" class="nav-link ">
<span class="title">All Pages</span>
</a>
https://laravel.com/docs/5.3/routing

You can try link with URL to like, I use in following manner
<a href="{{URL::to('backend/pages')}}" class="nav-link ">
<span class="title">All Pages</span>
</a>

Related

Route [transaksi.index] is not defined. but the route already exists with route:resource

I don't know why the error is being thrown, because the route already exists.
master.blade.php:
<!-- Nav Item - Transaksi -->
<li class="nav-item">
<a class="nav-link" href="{{route('transaksi.index')}}">
<i class="fas fa-fw fa-folder"></i>
<span>Transaksi</span></a>
</li>
web.blade.php:
Route::resource('transaksi','TransaksiController');
TransaksiController:
public function index()
{
$data = DB::table('tbl_transaksi')
->where('tbl_transaksi.nama_peminjam','like',"%{$request->keyword}%")
->paginate(20);
return view('admin.transaksi.index',['data'=>$data]);
}
The error:
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [transaksi.index] not defined. (View: C:\xampp\htdocs\SistemPerpustakaan\resources\views\admin\master.blade.php)
http://localhost:8000/dashboard
resolved, I just forgot to delete the route with the same name

Highlighting selected tab in nav - laravel blade

I have the code below in my view, but when i route to the page, the tab isn't showing as active. I search for this on answer on SO but it wouldn't solve my problem..
What am i not doing right?
I am using laravel version 5.6
VIew
<ul id="main-menu-navigation" data-menu="menu-navigation" class="nav navbar-nav">
<li class="nav-item {{ Request::path() == '/admin/dashboard/foods/all' ? 'active' : '' }}"><a href="/admin/dashboard/foods/all" class="nav-link">
<img src="/images/menubar/items.png" width="30px" height="30px" border="0" alt="Module Icon"/>
<span>Food Items</span></a>
</li>
</ul>
First i recommend you to put the logic in a directive, so the html look creaner and the logic is separated from the view.
In your AppServiceProvider you just put this
Blade::directive('menuActive',function($expression) {
//explode the '$expression' string to the varibles needed
list($route, $class) = explode(', ', $expression);
//then we check if the route is the same as the one we are passing.
return "{{ request()->is({$route}) ? {$class} : '' }}";
});
know in your view you just add
<ul id="main-menu-navigation" data-menu="menu-navigation" class="nav navbar-nav">
<li class="nav-item #menuActive('admin/dashboard/foods/all', 'active')"><a href="/admin/dashboard/foods/all" class="nav-link">
<img src="/images/menubar/items.png" width="30px" height="30px" border="0" alt="Module Icon"/>
<span>Food Items</span></a>
</li>
I'm not sure what Request::path() is returning, but it doesn't match your check for /admin/dashboard/foods/all (honestly, likely an issue with leading /)
Also, there is a method called is() on the request() helper that determines if the current route matches a given value, so use as:
<li class="nav-item {{ request()->is('admin/dashboard/foods/all') ? 'active' : '' }}"> ...
And it should work.

Laravel 5.6. How to switch languages with html anchor or other tag?

I searched for a solution in Google but I didn't found any specific. I'm new in Laravel so ... I'll explain my idea and will paste my code after that.
In the Header of my pages I have a few links for to switching the languages. Actually to be specific I have four countries flags as every flag respond for the specific language. I would like to change the language when user click on the German flag as an example. Then page need to be reloaded to update translations on it. This functionality works only in index Actions.
My Source Code: This is the html in my header located in app.blade.php
<div class="languages">
#if (Config::get('app.locale') == 'bg')
<a class="lang bg active" data-lang="bg" href="#">
<img src="{{URL::asset('/images/bg.png')}}" alt="bg" height="14" width="20">
</a>
#else
<a class="lang bg" data-lang="bg" href="#">
<img src="{{URL::asset('/images/bg.png')}}" alt="bg" height="14" width="20">
</a>
#endif
#if (Config::get('app.locale') == 'en')
<a class="lang en active" data-lang="en" href="#">
<img src="{{URL::asset('/images/gb.png')}}" alt="en" height="14" width="20">
</a>
#else
<a class="lang en" data-lang="en" href="#">
<img src="{{URL::asset('/images/gb.png')}}" alt="en" height="14" width="20">
</a>
#endif
#if (Config::get('app.locale') == 'de')
<a class="lang de active" data-lang="de" href="#">
<img src="{{URL::asset('/images/de.png')}}" alt="de" height="14" width="20">
</a>
#else
<a class="lang de" data-lang="de" href="#">
<img src="{{URL::asset('/images/de.png')}}" alt="de" height="14" width="20">
</a>
#endif
#if (Config::get('app.locale') == 'ru')
<a class="lang ru active" data-lang="ru" href="#">
<img src="{{URL::asset('/images/ru.png')}}" alt="ru" height="14" width="20">
</a>
#else
<a class="lang ru" data-lang="ru" href="#">
<img src="{{URL::asset('/images/ru.png')}}" alt="ru" height="14" width="20">
</a>
#endif
/Middleware/Local.php
<code>
<?php
namespace App\Http\Middleware;
use Closure;
use Session;
use App;
class Locale
{
public function handle($request, Closure $next)
{
$locale = Session::get('locale');
App::setLocale($locale);
return $next($request);
}
}
</code>
web.php
Route::get('/home', 'HomeController#index')->name('home');
Route::prefix('/home')->middleware('locale')->group(function() {
Route::get('/', function () {
return view('home');
});
});
Route::get('/switchLang/{lang}', 'SwitchLanguageController#switchLang')->name('switchLanguage');
app.js
$(function(){
var currentLanguage = document.documentElement.lang;
// Switch languages
$('.lang').on('click', function($event) {
$event.preventDefault();
var $selectedLang = $(this).data('lang');
$.ajax({
url: '/switchLang/' + $selectedLang,
type: 'GET',
success: function(response)
{
location.href = window.location.href;
}
});
});
});
SwitchLanguageController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
use App;
use Log;
class SwitchLanguageController extends Controller
{
public function switchLang($lang)
{
Session::put('locale', $lang);
$response = ['status' => 'success', 'code' => '200', 'message' => 'Language was switched.', 'metod' => 'GET'];
return $response;
}
}
I found the solution of my issue. The problem was in my routes in web.php.
This code didn't work for me on Laravel 5.6;
Route::get('/users/profile', 'UsersController#profile')->name('profile');
Route::prefix('/users/profile')->middleware('locale')->group(function() {
Route::get('/users/profile', function () {
return view('profile');
});
});
I replaced the above code with this one!
This work for me on Laravel 5.6:
Route::get('/users/profile', 'UsersController#profile')->name('profile')->middleware('locale');
Now Localization works for every route described as above. :)
Thank you very much for the help of #Devon.
The documentation of Laravel 5.6 really help me to fix the issue.
https://laravel.com/docs/5.6/middleware

Undefined variable in views laravel

I want to call variable to views in laravel, but something not working
here is my controller
public function index($id,$slugify, Request $request,WiuCookie $wcookie)
{
$user = ActionUsers::where('id', $id)->first();
$petition = Petitions::where('slug',$slugify)->first();
$isSigned = $wcookie->checkId($petition->id);
return view('layouts.master',compact('user','petition','isSigned'));
}
and here is my view
#if($isSigned)
<li>
<a href="#">
{{$user->name}}
</a>
</li>
#else
<li>
<a href="#">
register
</a>
</li>
<li>
<a href="#">
log-in
</a>
</li>
#endif
and error is Undefined variable: user
return view('layouts.master',compact(['user'=>$user,'petition'=>$petition,'isSigned'=>isSigned]));
It just works for me perfectly.
Try this:
$compactData=array('user','petition','isSigned');
return View::make('layouts.master', compact($compactData));
try this in passing remove compact and pass using with
->with('user',$user)

Laravel: Cannot route to expected URL

Here is my nav-bar:
<div class="col-md-2">
<ul class="list-group-item">
<li><i class="fa fa-fw fa-file</i> All Post
</li>
<li><i class="fa fa-fw fa-plus-circle"></i> Create New Post</li>
<li><i class="fa fa-fw fa-tasks"></i> Manage Posts</li>
</ul>
</div>
and here is my route.php
Route::group(['prefix' => 'posts'], function(){
Route::get('', 'PostController#index');
Route::get('create', 'PostController#create');
Route::post('confirm', 'PostController#confirmation');
Route::get('{postID}', 'PostController#show');
Route::get('posts/manage', 'PostController#manage');});
I expect when I click on the "Manage Posts" button, it will redirect me to function manage() in my PostController.
But when I click on it, it redirects to a view which belongs to storage/framework/views which is show() in my PostController.
I don't know why and how to make it to the right url.
Can somebody help me with this one please?
Thank you.
First of all, your link links to /posts/management, not /posts/manage. Second, you already have the prefix posts for this route-group, so the route posts/manage will be available under the url /posts/posts/manage.
You also want to move the manage route before your {postID} route, because {postID} will just catch anything, so the router has to first check the manage-route, and only if it doesn't match, the catch-all route.
And you should control what is accepted as a valid postID using Route Parameters: Regular Expression Constraints.
Route::get('{postID}', 'PostController#show')->where('id', '[0-9]+');

Categories