I have a methods that return base64 data image to a view after making a GET request to www.website.com/preview/{id}.
It is called by an <a> tag inside view.blade.php:
<a class="image-popup-vertical-fit" href="{{url(Config::get("app.previewPath") , $encrypted)}}" >
<img class="issue_img" src="{{App\Http\Classes\RepositoryUtil::getSmallImage($encrypted)}}" alt="{{ $name }}">
</a>
It work well if I declare a GET route with the code function inside routes.php:
Route::get(Config::get("app.previewPath") . "/{id}", function(\Request $request, $encrypted){
// ... some code ...
$base64 = \App\Http\Classes\RepositoryUtil::retriveImage($encrypted);
#readfile($base64);
});
But if I move the same code inside a controller's method, it return a blank page!
Route::get(Config::get("app.previewPath") . "/{id}", "MyController#getPreview");
MyController.php
public static function getPreview(\Request $request, $encrypted){
// ... same code as routes.php ...
$base64 = \App\Http\Classes\RepositoryUtil::retriveImage($encrypted);
#readfile($base64);
}
Where am I wrong?
I figure it out, it was a "distraction error".
I leave a middleware enabled in all methods of the controller, and it fails printing dd("Not authorized!"); without return before it.
It didn't return nothing without any errors!
I restrincted the middleware only to selected actions.
Thanks for support.
Related
I have sub folders inside my view folder called
--views
---saledata(folder)
----saled.blade.php
---uploader(folder)
----datauploader.php
---home.blade.php
I want to route this saled.blade.php and datauploader.php files in web.php
I have tried with
route:: view('saledata','../saledata/saled');
route:: view('datauploader','datauploader');
in both the ways.but both shows 404 errors.
and this is the way I mentioned the url of the file in home.blade.php
<a href="{{ url('saledata') }}" title="">
So please help to resolve this
if directly form the web file :
Route::get('saledata', function () {
return view('saledata.saled');
});
if want to use in the controller:
public function index(Request $request) {
return view('saledata.saled');
}
//this code will be the laravel v8
//Route File code
Route::get('saledata',[yourControllerFile::class,'your_controller_method']);
//Controller file code
class yourControllerName extends controllers {
public function your_controller_method() {
return view('saledata');
}
}
In route file:
Route::view('/saledata', 'saledata.saled')->name('saledata');
And in url:
<a href="{{ route('saledata') }}" title="">
Hope it will works.
In my web file, I have a route that accepts a $id as a value to be passed to a function within my PagesController. However, I want the function to still execute and show the intended form even when the $id is not passed to the function.
web.php file
Route::get('/request/{id}', 'PagesController#makeRequest');
PagesController.php file
public function makeRequest($id)
{
if(!empty($id)){
$target = Partner::find($id);
}
return view('pages.makeRequest')->with('target', $target);
}
makeRequest.blade.php
<input type="text" class="form-control" value="{{$target->inst_name}}" required disabled>
I want the page to display details from the database with the $id when provided or have empty spaces when the $id isn't provided.
As the Laravel Documentation states: Use Optional Parameters like this:
Route::get('/request/{id?}', 'PagesController#makeRequest'); //Optional parameter
Controller
public function makeRequest($id = null)
{
if(!empty($id)){
$target = User::find($id);
return view('pages.makeRequest')->with('target', $target);
} else {
return view('pageslist'); ///set default list..
}
}
This is the way I did it:
Route::get('index', 'SeasonController#index');
// controller
public function index(Request $request )
{
$id= $request->query('id');
}
The way you call it:
localhost/api/index?id=7
All your solutions were helpful. The main thing was that when I called just the view without passing $target to the view, the page displayed an error. So this is what I did.
Route::get('/request/{id?}', 'PagesController#makeRequest');
Then in the controller,
public function makeRequest(Request $request, $id=null)
{
if ($id != null) {
$target = Partner::find($id);
return view('pages.makeRequest')->with('target', $target);
}
return view('pages.makeNullRequest');
}
If you didn't understand what happened, I created a new view which had this instead of what I had posted in the question.
<input type="text" class="form-control" value="" required readonly>
Sorry I didn't update you guys in time. I think Jignesh Joisar came closest to helping me solve this issue. really appreciate all you guys. You're just awesome
You can use optional parameter :
Route::get('/request/{id?}', 'PagesController#makeRequest');
Now, as the parameter is optional, while defining the controller function you need to assign its default value to null in argument declaration.
<?php
public function makeRequest($id = null)
{
if($id){
$target = Partner::findOrFail($id);
return view('pages.makeRequest')->with(compact('target'));
}
// Return different view when id is not present
// Maybe all targets if you want
$targets = Partner::select('column1', 'column2')->get();
return view('pages.all')->with('targets');
}
I am using findOrFail instead of find. Its Laravel's very handy function which automatically throws a ModelNotFound exception and for frontend user throws a simple 404 page.
So if anyone is accessing www.url.com/request/2, its a valid id then it will show a valid page with data. If the accessed url is www.url.com/request/blahblah then it will throw 404. It avoids efforts of handling this manually.
For optional parameter pass id with ? in route and give $id = null in your function's parameter like this:
Route::get('/request/{id?}', 'PagesController#makeRequest'); //Optional parameter
makeRequest($id = null) {
// Code here...
...
}
in your routes file (web.php , as mentioned in your question)
Route::get('/request/{id?}', 'PagesController#makeRequest');
and in your controller PagesController.php
public function makeRequest($id = null)
{
}
To read more about this, just read https://laravel.com/docs/5.7/routing#parameters-optional-parameters
For me the answer was in the order that I listed the Routes in the routes file.
The routes file will call the first one that matches the pattern.
Route::get('/ohmy/{id?}', 'OhMyController#show');
Route::get('/ohmy/all', 'OhMyController#all'); //never gets called
Instead, put optional parameters at end of list:
Route::get('/ohmy/all', 'OhMyController#all');
Route::get('/ohmy/{id?}', 'OhMyController#show');
the answer has been said. just a side note: optional parameters won't work if you are using resource routes.
for example:
Route::resource('items',itemController::class)->except([
'create',
]);
Route::get('/items/create/{category_id?}',function($category_id = 'abc'){
dd($category_id);
});
if i go to " items/create/1 ", the result will be "1".
if i go to " items/create ", it will return 404. ( but we expect it to say "abc".)
this happens because other routes that start with "items" are expected to be generated from "resource" functionality.
so if you use resource routes, you should consider that.
I cannot figure out how to display the route name in the blade template of laravel. Please find below the sample code. Thank you.
from the controller (StaffsController.php)
public function index()
{
$thisRoute = Route::current()->uri();
return view('staff.list')>with(compact('thisRoute'));
}
Blade:
{{ $thisRoute }}
This is the var_dump
/home/vagrant/Code/spark/app/Http/Controllers/StaffsController.php:20:string 'staffs' (length=6)
Error:
(1/1) UnexpectedValueException
The Response content must be a string or object implementing __toString(), "boolean" given.
When i change the code in the controller to:
public function index()
{
$thisRoute = Route::current()->uri();
return dd($thisRoute);
}
I get "staffs" as output which is correct which is a string from the dump, right?
change your return statement to be like this :
return view('staff.list', compact('thisRoute'));
if you are using laravel 5.3 or above you can get route name like this:
Route::currentRouteName();
with this, you really don't have to passe it from your controller to the view just use it directly from your blade view :
{{ Route::currentRouteName() }}
Sorry guys, I solved the issue. I missed - in the line
return view('staff.list')>with(compact('thisRoute'));
Changed it to
return view('staff.list')->with(compact('thisRoute'));
I made a mistake.
Thank you
Would like to ask if anyone has ever tried to display an image from a Laravel Controller. Below is my code to a Laravel Controller. So basically I just want to hide the actual url of image and add additional validation so I decided to the image call my laravel URL.
Blade code that call the laravel controller
<img src="/image/1">
Route
Route::get('/image/{image_id}', ['as' => 'site.viewImage', 'uses' => 'ImageController#viewImage']);
Controller
public function viewImage($image_id)
{
return Storage::get($image_id . '.png');
}
But this return an error not-found. Am I doing something wrong here?
Note: I'm passing it to the controller because I need to do additional valdiation and to obfuscate the actual url of the file
I tried this code and its working but I would like a laravel type of approach
header("Content-type: image/png");
echo Storage::get($image_id .'.png');exit;
I also tried this approach
$response = response()->make(Storage::get($image_id . '.png'), 200);
$response->header("Content-Type", 'image/png');
return $response;
The laravel approach throws a 404 error.
Have you tried
return response()->file($filePath);
See: https://laravel.com/docs/5.3/responses#file-responses
If you want to show file from public folder
$link=url('link/to/image/'.$imageName);
header("Content-type: image/png");
$imageContent = file_get_contents($link);
echo $imageContent;
die();
Old question but you can do something like this in Laravel 9.x.
Blade:
<img src="/image/1">
Route:
Route::get('/image/{id}', [ImageController::class, 'viewImage']);
Controller:
public function viewImage($id)
{
// $image_path would look something like this: './images/abc.png'
$image_path = $id.'.png';
return response()->file( $image_path );
}
I have this problem in my routes where in I get this error tying to get property of non-object view (show.blade.php) on clicking a link to view/redirect allstats.blade.php. It is very weird because I am not returning my routes to show.blade.php but to allstats.blade.php. I was wondering if I am doing something wrong with the routes. This is very tricky, I don't know what's causing this error.
#Controller:
EmployeeController.php
public function postdisplaySummaryReport()
{
$employeesquery = Employee::paginate(10);
return View::make('employees.allstats')
->with('employeequerytoallstat', $employeesquery);
}
#Routes:
routes.php
Route::post('employees/sumarryReports','EmployeeController#postdisplaySummaryReport');
Route::resource('employees', 'EmployeeController');
#index.php(URL to the allstat.blade.php):
<span class="glyphicon glyphicon-folder-open"></span> {{trans('labels.payroll_reports.lbl_summary_report')}}
#ERROR:
Just change your function to this.
public function postdisplaySummaryReport()
{
$employeesquery = Employee::paginate(10);
return view('employees.allstats')
->with('employeequerytoallstat', $employeesquery);
}
also change your Route method to get instead of post if you want to display content.
Route::get('employees/sumarryReports','EmployeeController#postdisplaySummaryReport');