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

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>

Related

Route [controller.create] is not defined when I've already defined the Resource Controller in my Web.php

So I'm working with two Resource Controllers (I'm not sure if that's the problem but if it is, please let me know); one of them is StudentController and its URL is "/main". It's working as it should, perfectly. Now I'm creating the 2nd Resource Controller which name is TeacherController, and its URL is "teacher/main" - i.e its files are in the 'teacher' folder. This is my web routes file:
Web.php:
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('/main','StudentController');
Route::get('profile','ProfileController#index')->name('profile');
Route::get('profile/edit','ProfileController#edit')->name('profile/edit');
Route::put('profile/edit/{id}','ProfileController#update')->name('profile.update');
Route::resource('teacher/main','TeacherController');
Now when I go to teacher/main - it says that "teacher.main.create" is not defined. That is the route that I've used on my create button i.e
{{ link_to_route('teacher.main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}
And this is the create() function in my Resource Controller:
public function create()
{
return view ('teacher/create');
}
From my understanding of Laravel Resource Controllers, I shouldn't have to define each Resource Controller function individually as Laravel should automatically detect each of them since I've already defined Route::resource('teacher/main','TeacherController'); in my Web Routes. What am I doing wrong here? This is just the first function it's getting, then there are other functions like Edit/Destroy that will also pop-up route errors and I don't want to go through with defining each of them individually. Help is highly appreciated.
Try to use in group with prefix and as
Route::group(['prefix' => 'teacher','as'=>'teacher.'], function () {
Route::resource('main','TeacherController');
});
Now you can use teacher.main.create
{{ link_to_route('teacher.main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}
I just tested this locally, and seems like you're using wrong name for your resource route. I set the routes exactly as in your example, and this is what I got:
So if you wanted to use the create route, you'd do it like this:
{{ link_to_route('main.create','Add a Teacher','',['class'=>'btn btn-success float-right']) }}
On the other hand, if you want to keep the names you wanted, you can set a custom name for each of your resource route, like this:
Route::resource('teacher/main', 'TeacherController', [
'names' => [
'index' => 'teacher.main.index',
'create' => 'teacher.main.create',
// etc...
]
]);
Hope this helps. Happy coding.
Route::resource('teacher-main','TeacherController');
now you can access
teacher-main.index

How to get a laravel route group to work

I am trying to set up a Laravel route group in Laravel 5.5 and use it in a blade. However I am getting the a Route not defined error. The full error is :
"Route [admin/route_group_test] not defined. (View: C:\Users\Joey\Web\jrd_dnd_tools\resources\views\layouts\navigation.blade.php) (View: C:\Users\Joey\Web\jrd_dnd_tools\resources\views\layouts\navigation.blade.php) (View: C:\Users\Joey\Web\jrd_dnd_tools\resources\views\layouts\navigation.blade.php)
I have looked through the documentation and it looks like I am doing it right. Here is the line from the route file:
Route::prefix('admin')->group(function(){
Route::get('route_group_test','AdminController#testingMiddleWare');
});
and the link from the blade:
{{route('admin/route_group_test')}}
I have no idea what I am doing wrong
The route() helper uses route's name. From the docs:
The route function generates a URL for the given named route
So you need to name the route:
Route::get('route_group_test', 'AdminController#testingMiddleWare')->name('admin.route_group_test');
Or:
Route::get('route_group_test', ['as' => 'admin.route_group_test', 'uses' => 'AdminController#testingMiddleWare']);
And then use it:
{{ route('admin.route_group_test') }}
Or you can use unnamed route:
{{ url('admin/route_group_test') }}
Try the below code:
Route::group(['prefix'=>'admin','namespace'=>''], function () {
Route::get('route_group_test','AdminController#testingMiddleWare');
});

How to do a simple redirect in Laravel?

I have a function in Laravel. At the end I want to redirect to another function. How do I do that in Laravel?
I tried something like:
return redirect()->route('listofclubs');
It doesn't work.
The route for "listofclubs" is:
Route::get("listofclubs","Clubs#listofclubs");
If you want to use the route path you need to use the to method:
return redirect()->to('listofclubs');
If you want to use the route method you need to pass a route name, which means you need to add a name to the route definition. So if modify your route to have a name like so:
// The `as` attribute defines the route name
Route::get('listofclubs', ['as' => 'listofclubs', 'uses' => 'Clubs#listofclubs']);
Then you can use:
return redirect()->route('listofclubs');
You can read more about named routes in the Laravel HTTP Routing Documentation and more about redirects in the Redirector class API Documentation where you can see the available methods and what parameters each of them accepts.
Simply name your route:
Route::get('listofclubs',[
'uses' => 'Clubs#listofclubs',
'as' => 'listofclubs'
]);
Then later
return redirect()->route('listofclubs');
One method is to follow the solution provided by others. The other method would be, since you intend to call the function directly then you can use
return redirect()->action('Clubs#listofclubs');
Then in route file
Route::get("listofclubs","Clubs#listofclubs");
Laravel will automatically redirect to /listofclubs.

Laravel 4.2 link to action links & as %27

hello I have the following line:
{{ link_to_action('FriendController#add', 'Friend ' .explode(" ", $user->name)[0],[Auth::user()->id,$user->id]) }}
and a route defined as
Route::post('friend/add{id}&{fid}', ['as' => 'friend.add', 'uses' => 'FriendController#add']);
however the url isn't parsing the 2 arguments correctly as it comes up as /add1%262 instead of /add?1&2
I cant figure out why, it was working before. Also it comes up with method [show] does not exist when it does go through.
There are two problems here, that fail to match your expectations about what is supposed to happen:
1. The first and most importat one, is that the link_to_action helper function uses the to method in the Illuminate\Routing\URLGenerator class to generate the URL, which in turn makes use of the rawurlencode method that will encode reserved characters according to RFC 3986. Since the & character is a reserved sub-delimiter character it will automatically get encoded to %26, which will result in your URL path looking like this /add1%262.
2. The second issue is that you define your route path like this friend/add{id}&{fid}, yet you expect it to have a ? in there. Even if you were to add it the route definition like so friend/add?{id}&{fid}, it would still not work because ? is a delimiter character and will get encoded as well, so you'll end up with /add%3F1%262.
The central issue here is that you should not be defining query string parameters in your Laravel route definition. So you either move the parameters from the route definition to the query string and build your HTML link and the URL manually:
// routes.php
Route::post('friend/add', ['as' => 'friend.add', 'uses' => 'FriendController#add']);
// Your Blade template
<a href="{{ action('FriendController#add') }}?{{ Auth::user()->id }}&{{ $user->id }}">
Friend {{ explode(" ", $user->name)[0] }}
</a>
Or change the route definition so it doesn't contain any reserved characters that might get encoded:
// routes.php
Route::post('friend/add/{id}/{fid}', ['as' => 'friend.add', 'uses' => 'FriendController#add']);
// Your Blade template
{{ link_to_action('FriendController#add', 'Friend ' .explode(" ", $user->name)[0],[Auth::user()->id,$user->id]) }}
That being said, even with your current setup that generates the path /add1%262, Laravel would still know to decode the parameters, so in your controller action method you'll still get 1 and 2 as the user IDs:
public function add($id, $fid)
{
// $id = 1
// $fid = 2
}

Custom method instead of resource for Laravel routes

Using 4.2 and trying to add a custom method to my controller.
My routes are:
Route::get('ticket/close_ticket/{id}', 'TicketController#close_ticket');
Route::resource('ticket', 'TicketController');
Everything CRUD wise works as it should, but at the bottom of my TicketController I have this basic function:
public function close_ticket($id) {
return "saved - closed";
}
When I am showing a link to route on my page:
{{ link_to_route('ticket/close_ticket/'.$ticket->id, 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}
I constantly get a route not defined error, but it surely is defined...?
Any ideas where this is going wrong?
link_to_route expects a route name, not a url. This is why you are getting 'route not defined' errors, because you have not defined a route with the name you supplied to link_to_route. If you give your route a name, you can use link_to_route.
Given the following route definition, the name of the route is now 'close_ticket':
Route::get('ticket/close_ticket/{id}', array('as' => 'close_ticket', 'uses' => 'TicketController#close_ticket'));
The value for the 'as' key is the route name. This is the value to use in link_to_route:
{{ link_to_route('close_ticket', 'Mark As Closed', array($ticket->id), array('class' => 'btn btn-success')) }}
The laravel helper method link_to_route generates an HTML link. Which mean when clicked, the user will be performing a GET request.
In your routes file, you are defining this as a POST route.
Route::post(...)
Also, take a look at the docs for link_to_route here:
http://laravel.com/docs/4.2/helpers
You'll see that the first argument should be just the route name, without the ID appended.

Categories