I'm trying to access function in controller using wild cards in route through get call.
The route is defined on this way:
Route::get('/somefunc/{alias1}/{alias2}', 'uses'=>'MyController#myfunction']);
The route link I'm trying to access is defined here:
{{ __('Click') }}
But when I click on the link it gives me extra parameter in the route:
items/somefunc/somealiashere/1
because of the previous resource define in the web.php.
How to skip that 'items' parameter in the route.
Thank you.
Try to use url()
{{ __('Click') }}
Another way I recommend you is give name route.
Route::get('/somefunc/{alias1}/{alias2}', 'uses'=>'MyController#myfunction'])->name('somefunc');
and call it below.
{{ __('Click') }}
Related
Please, anyone, explain what is reverse routing with example.
I'm searching this question but still confused about this reverse routing concept.
For example the following route declaration tells Laravel to execute the action “signUp” in the controller “UsersController” when the request’s URI is ‘signUp’.
http://mycoolsite.com/signUp
Route::any('signUp’, 'UsersController#register’);
Traditionally, we may link to the registration page like this:
{{ HTML::link('signUp’, 'Register Now!’) }}
However, this has the unfortunate disadvantage of being dependent on our route declaration. If we change the route declaration to:
http://mycoolsite.com/signup
Route::any('register’, 'UsersController#signUp’);
Then our link will be wrong. We’ll have to go throughout our entire site and fix our links. Hope we don’t miss one!
Instead, let’s use reverse-routing.
{{ HTML::link_to_action('UsersController#signUp’, 'Register Now!’) }}
Now, the link that we generate will automatically change when we change our routing table. In our first example it’d generate http://mycoolsite.com/register. Then, when we change the routes call to match our second example it’ll generate http://mycoolsite.com/signup.
In traditional routing you depend on route declaration. In reverse routing on the some action(method, function)
In the route file, we define every route's name and use a complete website that routes names now in the future if we want to change a route URL then easily change it from the routes file. We change only one place and it applies the whole website by route names thus reverse routing makes development fast and flexible.
in Laravel 8, 9
Route::get('users', [UserController::class, 'index'])->name('user.index');
now link generate by route name
{{ route('user.index') }}
For example, I have a link to an other post like this in my template
My post
I want to be able to use the post object within the route helper to use the id.
Is there any syntax for this use case?
Or is there an other way of doing this?
Actually I had similar problems to solve this kind of cases. Your question related with Vue exactly, but the method below you can use for your case as well. Anyway you can't execute JS and PHP at the same time, cuz they're working at different sides. But as I also liked to have all routes with their aliases, I thought this approach.. You can imitate something like this:
Route::get('/', 'PostController#all')->name('all'); // all posts page
Route::get('post', 'PostController#all')->name('all_page'); // THIS IS THE THING (one additional route), WHICH WILL TAKE RESPONSIBILITY ON CASE, WHEN post_id WILL BE EMPTY
Route::get('post/{post_id}', 'PostController#post')->name('post');
This method will allow you to use the 1st and 3rd routes as normally, and as mixed too in the different places on your app like this:
{{ route('post.all') }}
{{ route('post', ['post' => $post_id]) }}
{{ route('post') }}/#{{ post.id }}
In scripts you can implement the approach like this:
let someUrl = "{{ route('post') }}/" + postObj.id;
In the view you can implement the method like this:
My post #{{ post.id }}
I am building a form using Form Model Binding via the Laravel Collective HTML package. The documentation (https://laravelcollective.com/docs/5.2/html#form-model-binding) boasts how the model's value is conveniently available with the value being set in the priority order of:
Session Flash Data (Old Input)
Explicitly Passed Value
Model Attribute Data
This is super useful because if a user has changed multiple fields, but 1 of them fails validation, you don't want all the other fields being reset to original value when they get thrown back to the form page with a helpful Message Bag of errors to give them a chance to correct their invalid input.
It's all very well using the Form::label, Form::text and Form::select helpers to leverage this lovely feature but what if you just want to access that convenient variable directly to do something a bit left-field?
Here's what I have tried...
{{ Form::model($user, array('route' => array('user.update', $user->id))) }}
{{ Form::label('first_name', 'First Name:', array('class' => 'address')) }}
{{ Form::text('first_name') }}
{{ Form::value('first_name') }} <-- No
{{ Form::session('first_name') }} <-- Nope
{{ Form::input('first_name') }} <-- Still no
{{ Form::attribute('first_name') }} <-- Absolutely not
{{ Form::close() }}
Help me, what is the method I am looking for?
The documentation hints at the ability to get the thing I want here: https://laravelcollective.com/docs/5.2/html#custom-macros where it says it's "easy" to define your own custom Form class helpers called "macros" but it doesn't actually tell you where you would write the code for this or where in the code you can look if you want to poke around and learn more about the FormBuilder class?
The method for doing this is Form::getValueAttribute('first_name') and the location for poking around to learn more is /vendor/laravelcollective/html/src/FormBuilder.php
I could get the current url using {{ URL::current(); }} in Laravel 4.2. But for some reason I need to get the current action, i.e. ControllerName#methodName. Please note that I don't want to generate a url to a controller action. How can I get the current action?
Depends, if you are only trying to get the action name, I think you can use Route method.
Route::currentRouteAction();
You always have access to current Route by calling
$currentRoute = Route::current();
which will probably tell you everything you need to know.
More on Route: http://laravel.com/api/4.2/Illuminate/Routing/Router.html
You may use currentRouteAction() method to get the current route action:
$action = Route::currentRouteAction();
In my view I do not want to hardcode a url in just incase I change it... Is there a way to generate the hyperlink url by saying i'm going to use this controller and this action... something like:
<a href = 'echo ActionLink("Logout", "Authentication");'>Logout</a>
I also just found this...
Logout
What you need to do is to be able to refer to your routes somehow. There are two primary methods of doing this: naming them and referring to a controller action (i.e. Controller#action).
The best and most flexible, however, is to name your routes. This means that if you refactor your controllers (e.g. change the classnames or namespaces), you have to change less code (just where the route points to, rather than where each view reference is).
Whichever way you do it, you can use all sorts of helpers to get what you want. The following are all equivalent:
{{ link_to_route('route.name', 'Title) }}
{{ HTML::linkRoute('route.name', 'Title') }}
Title
Title
Similarly, you can use 'action' in place of 'route' in those helpers to do the equivalent version using the Controller#action way of specifying the route.