Custom method instead of resource for Laravel routes - php

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.

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>

How would I define route to get me to a specific section on page in Laravel

I've a Register link on menu bar. On register page I have 2 forms, Login and Signup.
By default, it shows the Login form but I want it to show the Signup form first.
I thought If I do it with route by giving an Id to my signup form then I would be able to do that.
But it's giving me the error by saying
Missing required parameters for [Route: getRegister]
How would I modify my route?
Route
Route::get('/registration{url}', [
'uses' => 'niceActionController#getRegister',
'as' => 'getRegister'
]);
Controller
public function getRegister($url)
{
return view( '/' . $url, ['url' => $url]);
}
Header
{{ __('Register')}}
don't use the #url parameter in your web routes and controller, it is not meant for the backend.
Route::get('registration', ['niceActionController::class, 'getRegister'])->name('getRegister');
{{route('getRegister')}} will print out the link to the page, you'll want to put you parameter after the route, it will be handled after your page has loaded.
{{ __('Register')}}
in your controller you put the name of your blade template (not a route)
public function getRegister()
{
return view('nameofthetemplate');
}
Define your routes as below.
Route::get('action/{action_type}', 'NiceActionController#action') ->where('action_type', 'login|register') ->name("action");
In the view you can get route as below
{{route('action', ['action_type' => 'register'])}}
OR
{{route('action', ['action_type' => 'login'])}}
In the controller you can access the action_type parameter value as below.
$request->action_type;
Make sure you add Request $request parameter in method
Note:
The hash value is not accessible by the server script. You can either append action type in URL as shown in the above sample or include it in the query string. Check below link check how to get query string parameter value.
https://laravel.com/docs/8.x/requests#accessing-the-request
Tip: Please try to follow the Laravel naming conventions.
Because # symbol isn't parsed by the server, it's used by the browser for on-page navigation.
You might be familar with CSS/JS. # links in URLs will take you to either a named tag or ID tag in the page (e.g. http://page.com/#tag will take you to an element with the ID "tag")
When you hit http://page.com/registration/#register_forms the browser hit the page http://page.com/registration/ and searching for the id="register_forms" on your HTML.
Use this as your route
{{ __('Register')}}
Route::get('/registration/{url}', [
'uses' => 'niceActionController#getRegister',
'as' => 'getRegister'
]);
public function getRegister($url)
{
return view( viewNameHere, ['url' => $url]);
}

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 link to action not working without a defined route

I might be misunderstanding the link to route helper, but its not working without a route set up in my routes file.
{{ link_to_action('UserController#loginWithFacebook', 'Facebook Login in', $parameters = array(), $attributes = array('class' => 'btn btn-primary fb-login-btn')); }}
Then an old route when I was linking to a URI was:
Route::get('loginuser2', array('uses' => 'UserController#loginWithFacebook'));
However, I thought link_to_action was a direct call to the method. After removing the above link in my routes file I get not defined route errors for the controller method.
Any ideas how to avoid this?
You cant link to an action if the route itself does not exist. The route must be defined.
So you need to keep the route defined, and then link_to_action() will continue to work. On the backend it is looking at your routes to find the same route with that action - and uses that URL.
There is no way to avoid it.
link_to_action is used to link to a controller. you should use
link_to to generate html link
echo link_to('foo/bar', $title, $attributes = array(), $secure = null);

Laravel updating database route

I'm trying to update a column in my database with a total of votes on a project. I'm having a 404 problem but I don't see the problem in my route. (PS: Laravel 3)
This is my Vote button on every project :
{{ Form::open('project/addvote', 'VOTE') }}
{{ Form::hidden('id', $project->id) }}
{{ Form::submit('Vote') }}
{{ Form::close() }}
So when you click on the vote button it use this route :
Route::put('project/addvote', array('uses'=>'projects#addvote'));
And this is my action in the projects controller (no updating yet, just trying to redirect) :
public function put_addvote(){
return Redirect::to_route('project', $id)
->with('message', 'Vote successful');
}
Redirecting to this route :
Route::get('project/(:num)', array('as'=>'project', 'uses'=>'projects#project'));
And this is getting me the 404 error
Thanks to every response and the great help here!
Actually, Redirect::to_route expects a name of a route to redirect to it and a named route has to be declare with a name, like,
Route::put('project/addvote', array('as' => 'project', 'uses'=>'projects#addvote'));
So, you can use it;s name to redirect to it like
return Redirect::to_route('project');
Here, project has been used as it's name using 'as' => 'project'. In, your example, you didn't gave any name to the route, here
Route::put('project/addvote', array('uses'=>'projects#addvote'));
The, as => 'route_name' is missing.
For second question, you can do it as
$id = Input::get('id');
Project::find($id);
Project->votenumber = 5;
Project->->save();
Inserting & Updating Models.
Update :
It was a bit confusing but after a conversation through the commenting system the answer for routing is give below :
You (OP) mentioned that you have a route declared as
Route::get('projets', array('as'=>'projets', 'uses'=>'projets#index'));
To this route you are trying to redirect using this
return Redirect::to_route('project', $id);
So, you are passing a parameter and it's not in your route declaration and this is the problem, so to overcome this change your route declaration to this
Route::get('projets/(:num)', array('as'=>'projets', 'uses'=>'projets#index'));
Or
Route::get('projets/(:any)', array('as'=>'projets', 'uses'=>'projets#index'));
Or, you can make the param optional using a ?, for example :
Route::get('projets/(:any?)', array('as'=>'projets', 'uses'=>'projets#index'));
Check wildcard Routes.
Update :
You should have postd the original code with the question, anyways, Change this
return Redirect::to_route('project', $id);
to
return Redirect::to_route('project', array($id));

Categories