I would like to load all our translations into a flat array in resources/lang/en/messages.php and resources/lang/fr/messages.php
I would like to translate them in the view simply with trans('key') rather than trans('file.key')
Anyway to enable this behaviour? Seems it does not come out of the box. Thanks.
Figured out how, first define your own provider in App\Providers. Have it use your own custom class instead.
<?php namespace App\Providers;
use App\Utilities\TranslationUtility;
use Illuminate\Translation\TranslationServiceProvider;
class SimpleTranslationProvider extends TranslationServiceProvider {
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
$this->registerLoader();
$this->app->singleton('translator', function($app)
{
$loader = $app['translation.loader'];
$locale = $app['config']['app.locale'];
$trans = new TranslationUtility($loader, $locale);
$trans->setFallback($app['config']['app.fallback_locale']);
return $trans;
});
}
}
Custom class:
<?php namespace App\Utilities;
use Illuminate\Translation\Translator;
class TranslationUtility extends Translator {
public function get($key, array $replace = array(), $locale = NULL)
{
return parent::get('messages.' . $key);
}
}
Then add your service provider in config/app.php instead of 'Illuminate\Translation\TranslationServiceProvider'
Related
I want to modify Password Facade by adding methods to DatabaseTokenRepository and PasswordBroker
class DatabaseTokenRepository implements TokenRepositoryInterface
{
/*I want to add the method, reference to the exists() method*/
public function getRecord(CanResetPasswordContract $user)
{
return (array) $this->getTable()->where(
'email', $user->getEmailForPasswordReset()
)->first();
}
}
class PasswordBroker implements PasswordBrokerContract
{
/*I want to add the method*/
public function getTokenRecord(CanResetPasswordContract $user)
{
return $this->tokens->getRecord($user);
}
}
So I can use Password::getTokenRecord($user) to get the database record in the password_resets table.
I believe to use Password Facade, the code is more consistency, instead to use DB:table('password_resets')->where('email', $user->email)->first().
Is there a way to register these method? Or macro? Or classes which extend DatabaseTokenRepository and PasswordBroker, and then tell the Password Facade to use my extended classes?
Any suggestion? Thank you!
Thanks to the similar question, I solve the problem!
Here are my codes:
the custom method getTokenRecord() for me to use Password::getTokenRecord($user)
namespace App\Facades;
use Illuminate\Auth\Passwords\PasswordBroker as Broker;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class PasswordBroker extends Broker
{
public function getTokenRecord(CanResetPasswordContract $user)
{
return $this->tokens->getRecord($user);
}
}
the getRecord() which is called from the getTokenRecord() method
namespace App\Repositories;
use App\Models\Member;
use Illuminate\Auth\Passwords\DatabaseTokenRepository as DatabaseToken;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class DatabaseTokenRepository extends DatabaseToken
{
public function getRecord(CanResetPasswordContract $user)
{
return $this->getTable()->where(
'email',
$user->getEmailForPasswordReset()
)->first();
}
}
the custom PasswordBrokerManager to resolve my custom PasswordBroker and DatabaseTokenRepository
<?php
namespace App\Facades;
use App\Facades\PasswordBroker;
use App\Repositories\DatabaseTokenRepository;
use Illuminate\Auth\Passwords\PasswordBrokerManager as Manager;
use InvalidArgumentException;
class PasswordBrokerManager extends Manager
{
/**
* Resolve the given broker.
*
* #param string $name
* #return \Illuminate\Contracts\Auth\PasswordBroker
*
* #throws \InvalidArgumentException
*/
protected function resolve($name)
{
$config = $this->getConfig($name);
if (is_null($config)) {
throw new InvalidArgumentException("Password resetter [{$name}] is not defined.");
}
// The password broker uses a token repository to validate tokens and send user
// password e-mails, as well as validating that password reset process as an
// aggregate service of sorts providing a convenient interface for resets.
return new PasswordBroker(
$this->createTokenRepository($config),
$this->app['auth']->createUserProvider($config['provider'] ?? null)
);
}
/**
* Create a token repository instance based on the given configuration.
*
* #param array $config
* #return \Illuminate\Auth\Passwords\TokenRepositoryInterface
*/
protected function createTokenRepository(array $config)
{
$key = $this->app['config']['app.key'];
if (str_starts_with($key, 'base64:')) {
$key = base64_decode(substr($key, 7));
}
$connection = $config['connection'] ?? null;
return new DatabaseTokenRepository(
$this->app['db']->connection($connection),
$this->app['hash'],
$config['table'],
$key,
$config['expire'],
$config['throttle'] ?? 0
);
}
}
register my custom Password Facade
<?php
namespace App\Providers;
use App\Facades\PasswordBrokerManager;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Support\DeferrableProvider;
class PasswordResetServiceProvider extends ServiceProvider implements DeferrableProvider
{
public function register()
{
$this->registerPasswordBrokerManager();
}
protected function registerPasswordBrokerManager()
{
$this->app->singleton('auth.password', function ($app) {
return new PasswordBrokerManager($app);
});
}
public function provides()
{
return ['auth.password'];
}
}
comment out the origin PasswordResetServiceProvider::class from config/app.php, and add my custom class
'providers' => [
// Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
App\Providers\PasswordResetServiceProvider::class,
]
I am trying to add a shopping cart function to my Laravel application. I installed darryldecode/laravelshoppingcart package from GitHub and have been following instructions in these two websites.
TECHPOOL-Create a Shopping Cart with Laravel 6
LARASHOUT-Laravel E-Commerce Application Development – Checkout
I was able to create most of the shopping cart function with the first website but it didn't cover checkouts and placing orders so I found the second website.
The problem is that the contract file is not working. Here is the error I got.
Illuminate\Contracts\Container\BindingResolutionException
Target [App\Contracts\OrderContract] is not instantiable while building [App\Http\Controllers\CheckoutController].
http://localhost:8000/checkout
Where I use the contract file is in the checkout process witch is explained in the second website. I made few changes in the codes so that it will be consistent with the first website but mostly I followed what the website says.
Here are the codes that are mentioned in the error.
OrderContract.php
<?php
namespace App\Contracts;
interface OrderContract
{
public function storeOrderDetails($params);
}
CheckoutController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Contracts\OrderContract;
use App\Http\Controllers\Controller;
class CheckoutController extends Controller
{
protected $orderRepository;
public function __construct(OrderContract $orderRepository)
{
$this->orderRepository = $orderRepository;
}
public function getCheckout()
{
return view('checkout');
}
public function placeOrder(Request $request)
{
// Before storing the order we should implement the
// request validation which I leave it to you
$order = $this->orderRepository->storeOrderDetails($request->all());
dd($order);
}
}
OrderRepository.php
<?php
namespace App\Repositories;
use Cart;
use App\Models\Order;
use App\Product;
use App\Models\OrderItem;
use App\Contracts\OrderContract;
class OrderRepository extends BaseRepository implements OrderContract
{
public function __construct(Order $model)
{
parent::__construct($model);
$this->model = $model;
}
public function storeOrderDetails($params)
{
$order = Order::create([
'order_number' => 'ORD-' . strtoupper(uniqid()),
'status' => 'pending',
'grand_total' => Cart::getSubTotal(),
'item_count' => Cart::getTotalQuantity(),
'table_number' => $params['table_number'],
'name' => $params['name'],
'notes' => $params['notes']
]);
if ($order) {
$items = Cart::getContent();
foreach ($items as $item) {
// A better way will be to bring the product id with the cart items
// you can explore the package documentation to send product id with the cart
$product = Product::where('name', $item->name)->first();
$orderItem = new OrderItem([
'product_id' => $product->id,
'quantity' => $item->quantity,
'price' => $item->getPriceSum()
]);
$order->items()->save($orderItem);
}
}
return $order;
}
}
RepositoryServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Contracts\OrderContract;
use App\Repositories\OrderRepository;
class RepositoryServiceProvider extends ServiceProvider
{
protected $repositories = [
OrderContract::class => OrderRepository::class,
];
/**
* Register services.
*
* #return void
*/
public function register()
{
foreach ($this->repositories as $interface => $implementation) {
$this->app->bind($interface, $implementation);
}
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
}
I'm not really familiar with the contract concept since I only started learning Laravel recently and I'm completely lost here. Maybe the problem is that I haven't created another file that is necessary or maybe something else.
Any help would be appreciated as I have tried multiple methods with no success.
Thank you in advance.
Yes, this is expected...your contract should point to a Solid class else it's going to fail while trying to resolve it out of the container. So this is what you should do:
Create a class that implements that trait.
Go to your AppServiceProvider and bind it to that contract like this:
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
$this->app->bind(\App\Contracts\OrderContract::class, App\Repositories\ClassImplementingOrderContract::class);
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
}
this should fix your problem.
This error could caused even if you have forget to add "RepositoryServiceProvider" into "config/app.php" 's serviceProviders array.
I am following a tutorial to create a referal system in Laravel. In the tutorial it was not shown how to implement the addCredit() method of the user model class. I am a bit confuse. Assuming I have another table to keep the record of credits like :
user_credits
------------
user_id
credits
Is it good practice to write the code on user model's addCredits method to update the user_credits table? What will be the best in this case?
class User extends Authenticatable
{
/**
* Add bonus to the user
*/
public function addCredits($credit) {
//
}
}
The listener class to handle addition of the bonus for both the users.
namespace App\Listeners;
use App\Events\UserReferred;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
class RewardUser
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param UserReferred $event
* #return void
*/
public function handle(UserReferred $event)
{
$referral = \App\ReferralLink::find($event->referralId);
if (!is_null($referral)) {
\App\ReferralRelationship::create(['referral_link_id' => $referral->id, 'user_id' => $event->user->id]);
if ($referral->program->name === 'Sign-up Bonus') {
// User who was sharing link
$provider = $referral->user;
// add credits to provider
$provider->addCredits(15);
// User who used the link
$user = $event->user;
$user->addCredits(20);
}
}
}
}
I'm not pretty sure, is it good practice or not, but i prefer abstract such things into a standalone service.
In your case it would be something like that:
CreditService
namespace App\Services;
use App\User;
class CreditService
{
private $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function addCredits($credits)
{
$this->user->credits += $credits;
$this->user->save();
}
}
Then in controller/listener you can work with this service
use App\Services\CreditService;
...
public function handle(UserReferred $event)
{
$referral = \App\ReferralLink::find($event->referralId);
if ( !is_null($referral) ) {
\App\ReferralRelationship::create([
'referral_link_id' => $referral->id,
'user_id' => $event->user->id,
]);
if ( $referral->program->name === 'Sign-up Bonus' ) {
(new CreditService($referral->user))->addCredits(15);
(new CreditService($event->user))->addCredits(20);
}
}
}
The way how you make and then use service might be different. So, if you don't want work via constructors, you can write static class and pass User into method directly.
I often put some additional actions into services. For example, fire events when i need to do it. Or log some things.
In laravel we can use with() along with redirect(), like
return redirect('home')->with(['message' => 'Some message');
I want to create some other functions like withError(), withSuccess().
How and where to create this ?
As the Laravel RedirectResponse class uses the Macroable trait, you can register response macros to do this.
Just create a new service provider say ResponseMacroServiceProvider. Register it in your app.php and register a macro in the boot method like so:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Http\RedirectResponse;
class ResponseMacroServiceProvider extends ServiceProvider
{
/**
* Register the application's response macros.
*
* #return void
*/
public function boot()
{
RedirectResponse::macro('withError', function ($value) {
return; // add logic here
});
RedirectResponse::macro('withSuccess', function ($value) {
return; // add logic here
});
}
}
I have created trait as follows on this page app/Traits/ModelEventThrower.php
namespace App\Traits;
use Input;
use Event;
use App\Events\ActivityLog;
use Illuminate\Database\Eloquent\Model;
//use Illuminate\Support\Facades\Event;
/**
* Class ModelEventThrower
* #package App\Traits
*
* Automatically throw Add, Update, Delete events of Model.
*/
trait ModelEventThrower {
/**
* Automatically boot with Model, and register Events handler.
*/
protected static function bootModelEventThrower()
{
foreach (static::getModelEvents() as $eventName) {
static::$eventName(function (Model $model) use ($eventName) {
try {
$reflect = new \ReflectionClass($model);
echo "here";exit;
} catch (\Exception $e) {
return true;
}
});
}
}
/**
* Set the default events to be recorded if the $recordEvents
* property does not exist on the model.
*
* #return array
*/
protected static function getModelEvents()
{
if (isset(static::$recordEvents)) {
return static::$recordEvents;
}
return [
'created',
'updated',
'deleted',
];
}
}
My City Model is something like this
namespace App;
use App\Traits\ModelEventThrower;
use App\Events\ActivityLog;
use Illuminate\Database\Eloquent\Model;
use Event;
class City extends Model
{
use ModelEventThrower;
//protected static $recordEvents = ['updated'];
...
}
My CitiesController is
namespace App\Http\Controllers\Admin;
use App\City;
use App\Country;
use Input;
use Validator;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CitiesController extends Controller
{
......
public function update(City $city,Request $request)
{
......
$city->where('id','=',$input['id'])->update($input);
Somehow, I dont see its calling the function written in trait file. When I tried to create $city->create($input); it echos "here" and stops execusion, but not doing same for update function , however I could successfully update the records.
Any suggestion/help will be highly appreciated.
I had a similar issue with Laravel. By adding a constructor in the model to call the boot() function of the parent Model, like so:
public function __construct()
{
parent::boot();
}
you can make sure that all the traits are booted. This solved it for me.