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 }}
Related
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') }}
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 am having a problem passing a property using Vuejs ~1.0 to a child component from a Laravel Blade template. If I pass something in plain text it works just fine, but when I try to pass a js property, array, or object it doesn't work at all. I currently have a blade file with a custom component which looks like this:
<my-component video="#{{ stuff }}"></my-component>
If I leave out the #{{ }} the only thing that will be passed is the string stuff, and if I leave out the #, I obviously get a blade error, but if I use #{{ stuff }}, then all I get is the string {{ stuff }}. I'm obviously missing something, but can't tell where I'm going wrong. Thanks in advance.
Look like I just figured it out, it seems that I was missing the colon before video, so it should have appeared like so:
<my-component :video="stuff"></my-component>
If you are passing a variable to the component, then use:
<my-component :video= "{{ json_encode($stuff) }}" ></my-component>
Don't forget the double quotes or the result would be unpredictable for things like objects.
If you are passing model then do this:
<my-component :video="{{ $stuff->toJson() }}" inline-template></my-component>
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.
I'm using Silex, and need to alter a Twig file based on the location its loaded from.
My first thought was checking the url, but unfortunately this returns the location of the rendered Twig location, and not the parent url as expected.
{{ path(app.request.attributes.get('_route')) }}
Another attempt was to pass some vars with the render, however I'm not certain how to do this from within Twig (and if it's possible at all).
{{ render(path('cart')) }}
Can someone possible help me with a solution?
Thanks in advance
This is indeed a problem with silex core. I have submitted a pull request that introduces a new renderRoute twig function that should fix the issue. Hopefully it will be merged soon.
Well i didn't get perfectly what are you asking, but if you want to pass variables in render method you can do this:
{% render "ProjectTesteBundle::ControllerName::actionName" with { 'variableNameToPass' : variableValue } %}
if you want get route name in twig template you can do do something like this:
{% set routeName = app.request.attributes.get('_route') %}