Using Named URL in blade template - laravel-5

In Django, I can do this:
Account Link
which would give me domain/account/login where I have that URL named in my urls.py
url(r'^account/login/$', views.Login.as_view(), name='account_login'),
I want to do something similar in Laravel 5.2
I currently have something like this:
Route::get('/survey/new', ['as' => 'new.survey', 'uses' => 'SurveyController#new_survey']);
How do I use in my template, plus passing in parameters?
I came across this: https://laravel.com/docs/5.1/helpers, but it was just a piece of a white page without relevant content of how to actually use it.

You can use the route() helper, documented here.
My Link
If you include laravelcollective/html you could use link_to_route(). This was originally part of the core but removed in Laravel 5. It's explained here
{!! link_to_route('new.survey', 'My Link') !!}
The Laravel Collective have documented the aforementioned helper here. The function prototype is as follows
link_to_route($routeName, $title = null, $parameters = [], $attributes = []);
If for example you wanted to use parameters, it accepts an array of key value pairs which correspond to the named segments in your route URI.
For example, if you had a route
Route::get('surveys/{id}', 'SurveyController#details')->name('detail.survey');
You can generate a link to this route using the following in the parameters.
['id' => $id]
A full example, echoing markup containing an anchor to the named route.
{!! link_to_route('new.survey', 'My Link', ['id' => $id]) !!}

Related

laravel 5.1 : how can i use my route in blade?

I want to link to my route in blade. How can i do this?
<a href="roue" > go first</a>
if you define Route name you can use that in your blade :
define Route Name :
Route::get('/admin/transfer/forms-list', [
'as' => 'transfer.formsList',
'uses' => 'Website\TransferController#IndexTransferForms'
]);
now you can use that in your blade like this :
<a href="{{URL::route('transfer.formsList')}}" type="submit">
go first</a>
Of course if you use form collective you can use this :
{!! link_to_route('route.name', 'go first') !!}
There are few ways to use routes in a view.
The simplest one is using route() helper:
go first
If you're using Laravel Collective HTML & Forms, it will create full link, not only path to the route:
{!! link_to_route('route.name', 'go first') !!}
Or:
{!! HTML::linkRoute('mainpage', 'hey') !!}
Using URL facade. Wouldn't recommend, because you're getting redundant code:
go first

How to pass value from a button to a controller method in laravel 5

I working on a simple laravel 5.2 application. I want to pass a value $id from the view when a button is clicked to the controller method destroy(). This is what i have tried:
This is my route
Route::get('delete/{id}', array('as' => 'delete', 'uses' => 'ContactsController#destroy'));
... and this is my button in a view:
Delete
But this code didn't work. Thanks for any help.
According to the documentation you should be able to do this:
{{ action('ContactsController#destroy', ['id' => $contacts->id]) }}
Your route is expecting an id so you don't need to pass an identifier. The below code should work for you.
Make sure you're using the correct brackets as by default in Laravel 5.2 you'll need to use {!! !!} instead of {{ }}.
Delete
Since it is a GET request, you can simply have you own href instead of using blade. I just find this more readable:
Delete

How call route resource in Laravel

I have problem. My route definition contains:
route::resource('admin/settings/basic','admin\settings\BasicController');
but I don't know how can I call the edit action from basiccontroller in my a href link.
href='{{ link_to_route('admin/settings/basic/edit') }}'
Please give me some advice.
Given a route like the following:
app/Http/routes.php
Route::resource('profile', 'ProfileController');
Your controller could look something like this:
app/Http/Controllers/ProfileController
public function edit($id)
{
$profile = Profile::all(); // Grab some data
return view('profile.edit', [$profile]); // Pass some data to the Edit view
}
In the view, you might have a form for editing like so:
resources/views/profile/edit.blade.php
<?= Form::model($profile, ['route' => ['profile.update', $profile->id], 'method' => 'PUT', 'class' => 'form-horizontal']) ?>
That form routes to ProfileController#update
For other routes, such as an index, it is handled all for you. You just have to make sure you return the correct view in your ProfileController#index, and hitting the route for /profile will be passed through that method
You can always refer to the documentation as well - RESTful Resource Controllers

Laravel: URL::route() print only the route link without the website name

I have this route
Route::get('/dashboard', array(
'as' => 'dashboard-get',
'uses' => 'AppController#getDashboard'
));
In the View if i write
Dashboard
It will return me the entire link.
http://192.168.0.1/dashboard
How can get the route by name in the VIEW and only print the
/dashboard
Without the http://192.168.0.1/ part of the link
From the code source, route method generate an absolute URL by default, you may set it to false:
Dashboard
Update
You can also define your own custom links by
HTML::macro('Rlinks',function($routeName,$parameters = array(),$name){
return "<a href=".substr(URL::route($routeName,$parameters,false), 1) .">"
.$name.
"</a>";
});
Then call your macro
{{ HTML::Rlinks('dashboard-get',array(),'Dashboard') }}
You may try something like this:
app('router')->getRoutes()->getByName('dashboard-get')->uri();

Redirect to current URL while changing a query parameter in Laravel

Is there a built-in way to do something like this?
Let's say I have a search-page that has a few parameters in the URL:
example.com/search?term=foo&type=user
A link on that page would redirect to an URL where type is link. I'm looking for a method to do this without manually constructing the URL.
Edit:
I could build the URL manually like so:
$qs = http_build_query(array(
'term' => Input::get('term'),
'type' => Input::get('type')
));
$url = URL::to('search?'.$qs);
However, what I wanted to know is if there is a nicer, built-in way of doing this in Laravel, because the code gets messier when I want to change one of those values.
Giving the URL generator a second argument ($parameters) adds them to the URL as segments, not in the query string.
You can use the URL Generator to accomplish this. Assuming that search is a named route:
$queryToAdd = array('type' => 'user');
$currentQuery = Input::query();
// Merge our new query parameters into the current query string
$query = array_merge($queryToAdd, $currentQuery);
// Redirect to our route with the new query string
return Redirect::route('search', $query);
Laravel will take the positional parameters out of the passed array (which doesn't seem to apply to this scenario), and append the rest as a query string to the generated URL.
See: URLGenerator::route(),
URLGenerator::replaceRouteParameters()
URLGenerator::getRouteQueryString()
I prefer native PHP array merging to override some parameters:
['type' => 'link'] + \Request::all()
To add or override the type parameter and remove another the term:
['type' => 'link'] + \Request::except('term')
Usage when generating routes:
route('movie::category.show', ['type' => 'link'] + \Request::all())
You can do it with Laravel's URLGenerator
URL::route('search', array(
'term' => Input::get('term'),
'link' => Input::get('type')
));
Edit: be sure to name the route in your routes.php file:
Route::get('search', array('as' => 'search'));
That will work even if you're using a Route::controller()
From Laravel documentation:
if your route has parameters, you may pass them as the second argument
to the route method.
In this case, for return an URI like example.com/search?term=foo&type=user, you can use redirect function like this:
return redirect()->route('search', ['term' => 'foo', 'type' => 'user']);
Yes, there is a built in way. You can do your manipulation in Middleware.
The $request passed to the handle method of all middleware has a query property. As an InputBag, it comes with a few methods; Namely, for your intentions: ->set().
Pretty self explanatory, but here's an example:
public function handle(Request $request, Closure $next)
{
$request->query->set('term','new-value');
// now you pass the request (with the manipulated query) down the pipeline.
return $next($request);
}
The Input component should also contain query parameters.
i.e Input::get('foo');

Resources