Laravel 5 new custom message in lang folder - laravel

That later different languages ​​can be used, i would like want to save status and success messages in a central file and output them by the respective controller. Currently I'm still doing that in each controller extra.
At the moment:
return back()->with('status', 'Thanks for contacting us!');

Manually insert all the messages in directory resources > lang > en > messages.php , or create any other language folder you might want.
And then you can use it with this Lang::get('messages.success');
*use this in your controller use Illuminate\Support\Facades\Lang;
In your case like this: return back()->with('status', Lang::get('messages.success'));
An example of messages.php would be:
return [
'success' => 'your success message.',
'success1' => 'your success1 message.'
];

Related

File Upload in Laravel - Display Error to user if they are trying to upload big file

Within Laravel, I am successfully able to have a user upload a file on a page, but I want to know if there is a way for there to be an error displayed to that user before they submit the page that the file is too big. Something like "The file you selected to upload is 25MB. Get it below 20MB."
Is there some kind of package that can handle this?
Validating the file size on client side. (Mentioning this because you have mentioned that you would like to alert the error before form submit.)
Check the example code below which uses jQuery :
$(document).ready(function() {
$('input[type="file"]').change(function(event) {
var fileSize = this.files[0].size;
var maxAllowedSize = //add your value here;
// check the file size if its greater than your requirement
if(size > maxAllowedSize){
alert('Please upload a smaller file');
this.val('');
}
});
});
Validation on server side(you can change mime types as per the file type you want to allow) :
<?php
public function store(Request $request){
$request->validate([
'file_input_name' => 'file|max:25000|mimes:jpeg,bmp,png',
// add validations for other fields here
]);
}
For more check documentation
You don't need a package to do this, you can create either a Request class or use a validator:
1. Create a Request class:
Run the command php artisan make:request FileRequest
Then, on the File generated under App\Http\Requests\FileRequest do the following:
Change the authorize method to return true instead of false.
Under the rules method you return your validation rules:
return [
"file_input" => "max:20480", //If your input type's file name is "file_input"
];
According to documentation, max rule verifies that the input size from the user will not exceed the specified number in kilobytes for files.
2. You can also create a validator in your controller method:
use Validator;
public function store(Request $request)
{
$validator = Validator::make($request->only('file_input'), [
'file_input' => 'max:20480',
]);
if ($validator->fails()) {
return redirect()
->route('your.route.name')
->withErrors($validator)
->withInput();
}
// other code here
}

Http request and response in codeigniter

I am currently working in codeigniter and I am new to this.
I was wondering how to retrieve the JSON values using API call .
Can anyone suggest me where should I start.
Many Thanks in advance
Pass your array of row to json_encode();example for method of controller is below
public function getUserList() {
header('Content-Type: application/json');
$query = $this->db->get('mytable');
if(count($query) > 0) {
$message = array('status' => 'true' , 'message' => 'Record get successfully' , 'data' => $return );
}else{
$message = array('status' => 'false' , 'message' => 'Record not found.' );
}
echo json_encode($message);
}
Codeigniter does not have an inbuilt HTTP method so you need to use other things in php to achieve this.
There are 2 ways, you can use cURL, but honestly... it's convoluted... but read this: http://php.net/manual/en/book.curl.php
Another method is using stream_context_create() http://php.net/manual/en/function.stream-context-create.php
I strongly suggest using this 2nd one as its much easier to work with (in context with curl..
Much of how you setup your request depends on the API you are referencing with and the kind of requests it allows: GET, POST ... and what kind of header information it requires you to send over as well do they require oAuth header?
There is no 1 bunch of code fits all, I had to create a full custom library to integrate codeigniter into Magento, it took many hours.

Laravel before/after validation message

In Laravel you can make a custom messages for validators. But I found that the prepared messages are little bit wrong. With before and after validation rules, the parameter is converted with strtotime like that said in the documentation.
So if I set rule 'expires' => 'before:+1 year' the rule is working and correct. But if the user inputs a wrong value, Laravel prints the message like that:
The expires must be a date before +1 year
This message is very unclear for the average client. I was expected that the message will be converted with the strtotime also.
There is a clean way to print more clear error message?
You can override the validation messages with a custom ones if you want to.
In the Form Request class, add the following method and change the messages like so:
public function messages()
{
return [
// 'fieldname.rulename' => 'Custom message goes here.'
'email.required' => 'Er, you forgot your email address!',
'email.unique' => 'Email already taken m8',
];
}
Update:
If you want to change the global messages, you can open the validation.php file inside resources/lang/en/validation.php and edit the message there.
You can user AppServiceProvider.php
and you can write your new validation rule there and set your error message in validation.php file
/app/Providers/AppServiceProvider.php
/resources/lang/en/validation.php
Ex:
Validator::extend('before_equal', function ($attribute, $value, $parameters) {
return strtotime(Input::get($parameters[0])) >= strtotime($value);
});
and the message is
'before_equal' => 'The :attribute field is only accept date format before or equal to end date',
Please try this and you can alter this to based on your require.

Laravel - Getting string from a flash

After processing form input, I redirect to a new route with some flash data:
return Redirect::route('work.index')
->with('flash', 'New work entry has been entered');
In the controller specified by work.index, I try to access the data
$flashed = Session:get('flash');
However, instead of a string, I end up with an array with two sub-arrays, old and new
Am I doing something wrong? Am I supposed to do this?
$flashed = Session::get('flash')['new'][0]
Store Data for next request
Session::flash('city', 'New work entry has been entered');
Retrieve Data from last request
$data = Session::get('city');
return Redirect::route('work.index')
->with('data', $data);
My Advice is to use Laracasts/Flash package that helps you to manage Flash messages in an easy way.
Here the GitHub repo: https://github.com/laracasts/flash
Installation
First, pull in the package through Composer.
"require": {
"laracasts/flash": "~1.0"
}
And then, if using Laravel, include the service provider within app/config/app.php.
'providers' => [
'Laracasts\Flash\FlashServiceProvider'
];
And, for convenience, add a facade alias to this same file at the bottom:
'aliases' => [
'Flash' => 'Laracasts\Flash\Flash'
];
And you can use it with:
Flash::info('Message')
Flash::success('Message')
Flash::error('Message')
Flash::warning('Message')
Flash::overlay('Modal Message', 'Modal Title')
Now in your theme you can easly integrate it with:
#include('flash::message')
NB:
Note that this package is optimized for use with Twitter Bootstrap.

Codeigniter basic delete

I have a link that calls this function:
function delete($id)
{
//Delete from database
$this->db->delete('messages', array('id' => $id));
$data['delete_message'] = 'Message was successfully deleted';
redirect('admin');
}
As you can see I redirect to the admin function, and I want to pass the delete_message to that function. How can I do this?
Have you looked at "Flashdata"?. You can set your success message in the flash and the next page (admin in your case) reads it if available and passes it to the view as a regular $data['foo'].

Resources