This is in my blade file
{!! Html::link(storage_path().'/documents/'.$file->name, $file->name) !!}
This is controller
public function download($file_name){
$file_path = storage_path('documents').'/'.$file_name;
return response()->download($file_path);
}
Route
Route::get('documents/{file}','FilesController#download');
You want to add your exact route to your HTML link instead of the path of the file:
{!! Html::link('/documents/'.$file->name, $file->name) !!}
The proper way of using HTML::link() is like this:
{{ HTML::link(storage_path().'/documents/'.$file->name, 'Download Link')}}
and this would produce the HTML like this:
Download Link
See more about HTML::link()
You can also use route() helper method like this:
Just add a name() method in-front of the route method, as it gives route a common name.
Route::get('documents/{file}','FilesController#download')->name('downloads.link');
HTML should look like this:
Download Link
Related
I have route file defined as follows...
Route::get('pincodes/export', 'PincodeController#export'); Route::resource('pincodes','PincodeController');
I have a controller called 'PincodeController' where the default functions works without any issue. I wanted to add a new function called 'export' in this controller. I tried {{ route('pincodes.export') }} in my blade file to generate link to this page... and it throws the following error...
Facade\Ignition\Exceptions\ViewException Route [pincodes.export] not defined. (View: /Users/tst/Desktop/www/laravel/project/resources/views/pincode/index.blade.php)
but if i access the url directly in browser it works fine. why is that ?
You need to specifically name the route:
Route::get('pincodes/export', 'PincodeController#export')->name('pincodes.export');
If you are using Laravel 5.5+, change export route like this
Route::get('pincodes/export', 'PincodeController#export')->name('export');
use route function in your blade file like
route('export')
I got a question using laravel, I am trying to call a function and passing some parameters along with it using the {{ route }} method something along these lines here:
<img src="{{route('cacheImage', ['general', 'mini-logo.png']) }}" />
My route in my web.php file looks like this:
Route::get('/cache/images', ['uses'=>'HomeController#cache_image','as'=>'cacheImage']);
As a result, I am getting something like this here when the page loads in my img src:
http://localhost:8000/cache/images?general&mini-logo.png
But this is not what I want, what I would like is to have something along these lines here in my img src:
http://localhost:8000/cache/images/general/mini-logo.png
At the same time I want to call the cacheImage function in my controller and pass it both parameters, which are "general", and "mini-logo.png"
How do I achieve this? Can someone give me an example?
Change your route to this.
Route::get('/cache/images/{type}/{name}', [
'uses' => 'HomeController#cache_image',
'as' => 'cacheImage'
]);
Controller method to this to be able to receive the url segments as parameters.
public function cache_image($type, $name)
{
dd($type, $name);
}
Then you can generate links like so.
$link = route('cacheImage', ['general', 'mini-logo.png']);
// generates http://example.dev/cache/images/general/mini-logo.png
I have created a small test-project and wanted to use this:
https://github.com/vluzrmos/collective-html
I have done the same things step by step as in the manual. But I got this error:
Whoops, looks like something went wrong.
In the app.php I enabled this:
$app->withFacades();
$app->withEloquent();
And added the register and alias as given in the manual. Also I changed in the index.php that the requests are working.
My route and my controller are working. If I write only html content the page is fully and correctly shown.
But If I change my blade-template and at this:
{!! $form->open() !!}
{!! $html->asset() !!}
It doesn't work. And the error which I have given before occurs and the page is not shown.
Is the call wrong or should I declare anything in my Controller or in the head of the View?
By the way, the files/library are/is existing and loaded via composer
(Lumen 5.2)
to use this way:
{!! $form->open() !!}
you need to put this on bootstrap/app.php
$app->register('Collective\Html\InjectVarsServiceProvider');
Or you can just use like this:
{!! Form::open(array('url' => '/test')) !!}
In my Symfony2 project, I would like to get the HTML of a file and put it in a variable in my controller.
I have a controller function like this:
public function myControllerAction()
{
$htmlOtherFile = '';
return $this->render('#MyBundle/myTargetFile.html.twig', ['htmlOtherFile' => $htmlOtherFile]);
}
The html file I would like to import is in my views folder: htmlOtherFile.html.twig
How can I get the HTML of this file into my $htmlOtherFile variable? I have already tried with file_get_contents, but without success.
all you need to do is call renderView from your controller on the template and put the contents in a variable.
$html = $this->renderView('/path/myTargetFile.html.twig', [/*options in here*/]);
Be sure to call renderView and not render on its own as that will return a Response instance, and not any HTML.
Alternatively, you can call:
$this->render('/path/myTargetFile.html.twig', [/*options in here*/])->getContent();
to return the html from the response.
If it is twig file - you could try this:
$this->renderView('/path/to/template.html.twig', []);
You can call a controller directly from you twig file if you want.
Use the syntax {{ render(controller('MyBundle:functionName')) }}
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);