route() returns different way in two route - php

I made two resource routes and tied them into a group.
Route::group(['middleware' => 'auth'], function () {
Route::resource('user/posts', PostController::class)->name('show', 'user.posts');
Route::resource('admin/posts', PostController::class)->middleware('is_admin')->name('show', 'admin.posts');
});
The href tag below code only works in admin, which turns to '/admin/posts'. but when a not admin user is logined, It points '/posts' not '/user/posts'/.
<x-jet-nav-link href={{route('posts')}}" :active="request()->routeIs('posts')">
{{ __('Posts') }}
</x-jet-nav-link>
So I had to change the code like below.
<x-jet-nav-link href="{{ route(auth()->user()->isAdmin?'admin.posts':'user.posts', '') }}" :active="request()->routeIs('posts')">
{{ __('Posts') }}
</x-jet-nav-link>
what did I miss on the first method?
Additionally :active= is not working. How can I fix that?

Can you run php artisan route:list and post the output?
And for the second part with the href for the link, I think you need to specify the route like posts.index or posts.show and for the isActive try: :active="request()->routeIs('posts.*')"

Related

Laravel - Conditional Route Name mapping / dynamic routes

I thought to write static routes like contact, imprint or "About Us" in the web.php as a one-liner. I saw this at Laravel Daily.
web.php
Route::get('/{page}', App\Http\Controllers\StaticPageController::class)
->name('page')
->where('page', 'about-us|imprint|contact');
It's nice but I'm getting problems with my navbar.
My Blade navbar has a dynamic part. The current menu item is highlighted. Very simple.
nav.blade.php
<x-nav-link :href="route('about-us')" :active="request()->routeIs('about-us')">
{{ __('About us') }}
</x-nav-link>
With the new one-liner, I then get the following error message:
Symfony\Component\Routing\Exception\RouteNotFoundException.
Route [about-us] not defined. (View: /project01/resources/views/includes/nav.blade.php)
Which is logical, because I no longer have the about-us route.
Actually, the route should be given a corresponding array mapping. But I don't know how. How can I solve the problem?
The solution for this problem will be:
<x-nav-link :href="route('page', ['page' => 'about-us'])" :active="request()->routeIs('about-us')">
{{ __('About us') }}
</x-nav-link>
Special Thanks for all good answears!
You have two options.
1- Change the routeIs() to is() and use the path, not the route name
<x-nav-link :href="route('page', ['page' => 'about-us'])" :active="request()->is('about-us/*')">
{{ __('About us') }}
</x-nav-link>
2- Switch back to 3 routes instead of the one liner. There is no gain in using the one liner. It is slower and harder to read (maintain)
You have assigned a name page to a given route with the required parameter named page. So, you must use the following code:
route('page', ['page' => 'about-us']);
https://laravel.com/docs/6.x/routing#named-routes
I think there is a misunderstanding.'about-us|imprint|contact' is the parameter restrictions.'page' is the route name.So this should work;
<x-nav-link :href="route('page','about-us')" :active="Request::url() == route('page','about-us')">
{{ __('About us') }}
</x-nav-link>

Laravel 5 GET inside controller?

I use Laravel5 and i cant understand why postProcess or getProcess not works?
Example::
html page:
{{ Form::open(array('url' => 'portfolio/process')) }}
{{ Form::submit() }}
{{ Form::close() }}
route:
Route::resource('portfolio','PortfolioController');
controller:
public function postProcess (){
return 'Text!';
}
Every time i get error:
MethodNotAllowedHttpException in RouteCollection.php line 218:
It doesn't work because Route::resource doesn't build these routes and you need to explicitly define them:
Route::post('portfolio/process', 'PortfolioController#postProcess');
I think you need to check your route list:
Run this command php artisan route:list in terminal and check your route.
Hope this work for you!

understanding how laravel interprets blade form url?

I have the below form in a larvel view:
<div id="admin">
<h1>Products Admin Panel</h1><hr>
<p>Here you can view, delete, and create new products.</p>
<h2>Products</h2><hr>
<!--admin/fileupload/create-->
{{ Form::open(array('url'=>'admin/products/create' , 'files'=>true)) }}
<p>
{{ Form::file('image') }}
</p>
{{ Form::submit('Create Product' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
</div> <!-- end admin -->
I am new to laravel and basically just want to understand , in the URL when i specify 'url'=>'admin/products/create' , what is laravel going to look for ?
a modal called products ? or a controller called products ? and a method getCreate or postCreate inside it ? what is admin then , i want to understand how laravel interprets this blade form url , can anybody explain ?
I want somebody to explain to me how does laravel interpret blade form url ?
In laravel,you will specify which url to be processed by which controller and by which method inside that controller.This specify must be do in routes.php file that is located in projectname/app/Http/routes.php .
When you specify the 'url'=>'admin/products/create' you must define the route in routes.php .
Route can be define in different ways like:
Route::get('admin/products/create','ProductController#crete');
Here you can use get or post according to your request.
Another way you can do is
Route::get('admin/products/create',array(
'as'=> 'create-product',
'uses'=>'ProductController#create'
));
Now , you can do like this route('create-product'); instead of 'url'=>'admin/products/create' .
Another way by using Route group
Route::group(['prefix'=>'admin'],function(){
Route::group(['prefix'=>'products'],function(){
Route::get('/create',array(
'as'=>'create-product',
'uses'=> 'ProductController#create'
));
// Here you can define other route that have the url like /admin/products/*
});
});
Now you can do like route('create-product') or 'url'=>'admin/products/create' Advantage
For more info check the documentation Here

how to use named routes in view where named routes have wildcard

I've a group routes like this
Route::group(['prefix' => 'admin/{username}'],function()
{
Route::get('', ['as' => 'dashboard', 'uses' => 'AdminController#index']);
Route::get('settings', ['as' => 'settings', 'uses' => 'AdminController#settings']);
});
In my view i used named routes for link reference like this
<li><span>Settings</span></li>
but it's rendering as
<li><span>Settings</span></li>
the username is printing literally but not the actual value. What i really want is this
<li><span>Settings</span></li>
How to do this?
You should try following...
<li><span>Settings</span></li>
You would have to pass current username as second parameter.
You have to pass route parameters when generating an URL. Like:
{{ route('settings', ['john']) }}
Obviously you would rather do something like:
{{ route('settings', [$user->name]) }}
Since you only have one parameter you don't even have to pass it as array:
{{ route('settings', $user->name) }}
And thus the address looks better you can use strtolower() if you have a username with a capital letter.
<li><span>Settings</span></li>
Caution
Auth::user() is for the current user. If no one is logged in you get an error!
So you can do a check.
#if(Auth::user())
<li><span>Settings</span></li>
#endif
Another way to do that is passing the wild card to view via controllers and use it in route as second parameter
first pass the wildcard in controller
public function index($username)
{
return view('admin.dashboard', compact('username'));
}
and in view pass the variable in routes as second parameter
<li><span>Settings</span></li>

Laravel 4 - Multiple forms on same page?

I'm having some strange behavior with my forms in Laravel 4. I have a "settings" page with two forms, each (are supposed to) POST to a controller method, update the database and return back to the settings page. However, there seems to be an issue, either with the way my forms are working or my routes.
Here's how it is, simplified:
Settings page: (site.com/settings)
<div id="form-one" class="form-area">
{{ Form::open(array('action' => 'SettingController#editOption')) }}
{{ Form::text('optionvalue', 'Default')) }}
{{ Form::submit('Save Changes') }}
{{ Form::close() }}
</div>
<div id="form-two" class="form-area">
{{ Form::open(array('action' => 'SettingController#editPage')) }}
{{ Form::text('pagevalue', 'Default')) }}
{{ Form::submit('Save Changes') }}
{{ Form::close() }}
</div>
So basically, two seperate forms on the same page that post to two seperate methods in the same Controller - when the method is successful, it redirects them back to "settings". I won't post the methods, since tested them and they work, I believe the problem is in the routes file:
routes.php
// Checks if a session is active
Route::group(array('before' => 'require_login'), function()
{
Route::group(array('prefix' => 'settings'), function()
{
Route::get('/', 'SettingController#index');
Route::post('/', 'SettingController#editOption');
Route::post('/', 'SettingController#editPage');
});
});
Now I'm pretty sure it doesn't like the two POST routes being like that, however I cannot think of another way to do it, since the forms are on the same page. I get the error:
Unknown action [SettingController#editOption].
Since the option form comes first I guess. If I take the open form blade code out (for both), it loads the page - but obviously the form doesn't do anything.
Any help would be nice! Thanks in advance.
You can't add two same routes for different actions, because of they will be passed to first matched route and in your case to SettingController#editOption. Change your routes to :
Route::post('/option', 'SettingController#editOption');
Route::post('/page', 'SettingController#editPage');
Than in both actions you can redirect to '/': return Redirect::back(), and if error was occured:
if ($validation->fails())
{
return Redirect::to('/settings')->with_errors($validation);
}
My alternative solution for this is to create an hidden html input in each form and make the controller identify what for is submitted based in this field. So, yu can use just one route for both.

Categories