laravel Response:download()->with() not working - laravel

return response()->download($resultPath."/".$csvfilename,$csvfilename, $headers)->with("success","success"); not working
how to pass parameter in return in laravel

Maybe you should be able to do return response()->with('success', 'success')->download(...) but I'm not sure.
If you really need to check the success, try to set a header or to check the HTTP status code.

Related

laravel request return message keys translations

I have a Laravel Application and another APP making calls via API to Laravel. These 2 projects are separated.
Laravel and App have their own multilanguage system. They work independently but uses the same key translations.
So my idea was that all Laravel responses must be translations key, like: 'messages.success'.
With this response, the App can translate it.
All of these are working fine.
The problem appeared when I started working with Laravel Requests for validating forms.
In this case, the validation errors are automatically translated so the App receives the response translated with the default language of the Laravel application.
So what can I do?
I thought with 2 ideas but I don't know if they can work.
1: Passing the language into params. Don't know if it can work, how can I set the language before Laravel validates the Request?
2: Override the functionality of Requests to return messages without translate, so instead of "Felicidades" return "messages.success". I really like this approach. But how can I do it for all the rules? Overriding the messages function like this:
public function messages()
{
return [
'unique' => 'validation.unique'
];
}
For every rule works... but I feel bad.
Another approaches?
What is the best way to fix this problem?
I would suggest that you use this hacky solution in 2 lines of code. Go to /resources/lang/{code}/validation.php. You can see that it returns an array of messages by default. Modify it like so:
// Replace return in the first line
$ret = [
/* all the translations go here as normal */
];
// Add this as the last line. This will replace all values with their keys.
return array_combine(array_keys($ret), array_keys($ret));
After that you can use validation as per usual and you'll get validation message keys instead of messages. Cheers and hope this helps.

Laravel 5 set route conditionally

Hi I want to get specific route only based on the condition. I currently tried Session but it doesn't work with routes. So anyone here might want to help if there's simpe way to this.
if(\Session::get('quiz_type') == 'quiz'){
Route::resource('quizzes.questions', 'QuestionsController');
}else{
Route::resource('surveys.questions', 'QuestionsController');
}
I want that certain route Quiz only if I passed and meet a certain condtion. Else I want to call on different route.
If I remember correctly Session start only after laravel has already parsed the routes file, this is why your code doesn't work as you expect.

Detect if running from middleware in Laravel

Is there a way to detect if my code is running from a Middleware. I have a helper that get's called from everywhere for date conversions. In that helper, I check for route names \Route::current()->getName() or if running from console \App::runningInConsole().
When my middleware calls the helper, I get an error with \Route::current()->getName() since \Route::current() is null
Is there a way of knowing if code executed from my middleware?
Thanks
Problem solved
I had problems integrating Cashier and middleware and got that fixed, and to prevent going to my timezonehelper to set dates, I used the protected $dates = ['trial_ends_at', 'ends_at'];
Thanks for your help
You can get full url in laravel using the code below
$fullUrl = Request::capture()->fullUrl();
And You can get current url in laravel using the code below
$currentUrl = Routes::current()->getName();
In this case, you have to use this namespace given below:
use Illuminate\Support\Facades\Route as Routes;

PHP after sending response send another link

one of my system needs to implement such a ways, that after getting on my link i replied a
return response('OK', 200);
or
return response('Unauthorized', 401);
Now, i need to implement such a ways that after returning ok response, i need to go through another link. How am i supposed to implement that? I am using php laravel. I am kind of unsure how these will work. Will i get to go to another laravel route for that or any other ways?
Thanks.
Try:
return response()->setStatusCode(200, 'OK');
and
return response()->setStatusCode(401, 'Unauthorized');
For redirection you can use redirect() function.Or if response ok then you can go for redirect another route what you want.
return redirect('user/login')->with('message', 'Login Failed');

Backbone.js PUT/DELETE problems with Codeigniter REST server

NOTE: This question is related to CodeIgniter-RestServer
When I call model.save() from backbone the function where the put request is routed doesn't gets any PUT data. Firebug shows right PUT parameters being sent. However $this->put('keyname') always returns false. Which means CI's REST Server can't find PUT data as it should.
On the other hand, If I set:
Backbone.emulateJSON = true;
I can work, as then Backbone will send all PUT data under a single attribute named "model", using this way $this->put('model'); works
Then the extra effor involved is:
$data = json_decode($this->put('model'),true); // to get normal behavior #sucks
I was running into this issue as well and pushed a few changes that fix the problem:
https://github.com/philsturgeon/codeigniter-restserver/pull/84
have been through this problem already in the past. Solution to this problem is to use this inside your functions:
$data = $this->request->body;
echo $data['id'];
Hope that solves it. Cheers!

Resources