Symfony method return null - php

There is a method that takes data from the base value, and outputs it.
If you go through some issues app.php - give NULL, and when entering through app_dev.php - gives the correct boolen value.
P.S function name: isBlocked()
public function indexAction($slug)
{
$product = $this->get('manager.shop.product')->getBySlug($slug);
if (!$product) {
throw $this->createNotFoundException();
}
$this
->get('util.breadcrumbs')
->add('Catalog', $this->generateUrl('categoryIndex'))
->add(
$product->getShop()->getName(),
$this->generateUrl('shopIndex', ['slug' => $product->getShop()->getSlug()])
)->add(
$product->getName(),
$this->generateUrl('productIndex', ['slug' => $product->getSlug()])
);
$this->get('manager.shop.product')->hit($product, $this->getRequest());
foreach ($product->getImages() as $image) {
$this->get('service.image')
->create($image)
->thumbnailize('525x500-800');
}
$shop = $this->get('manager.shop')->getBySlug($product->getShop()->getSlug());
print_r(var_dump($shop->isBlocked()));
return $this->render('MashApplicationBundle:Product:index.html.twig', [
'anotherProducts' => $product->getAnotherShopProducts()->slice(0, 3),
'product' => $product,
'shop' => $shop,
// 'isBlocked' => $shop->isBlocked(),
'addToCartForm' => $this->createForm('checkout', null, ['data' => ['products' => [$product->getId()]]])->createView()
]);
}

Solve:
service memcached restart
in SSH

Related

Too few arguments to function App\Http\Controllers\RaveController::addToOrdersTables(), 0 passed and exactly 2 expected

Hi I am using RaveFlutterWave as my payment gateway. I want to store an orders whenever a customer finish making payment but I couldn't get pass that error. I don't know what am missing there.
Thanks for your help, here is my code.
public function callback(Request $request)
{
// $data = Rave::verifyTransaction(request()->txref);
$resp = $request->resp;
$body = json_decode($resp, true);
$txRef = $body['data']['data']['txRef'];
$data = Rave::verifyTransaction($txRef);
return redirect()->route('success');
}
Here is my route
Route::get('/success', 'RaveController#addToOrdersTables')->name('success');
and this is my method for saving the order
protected function addToOrdersTables($request, $error)
{
$order = Order::create([
'user_id' => auth()->user() ? auth()->user()->id : null,
'billing_email' => $request->email,
'billing_first_name' => $request->first_name,
'billing_last_name' => $request->last_name,
'billing_address' => $request->address,
'billing_city' => $request->city,
'billing_town' => $request->town,
'billing_postalcode' => $request->postalcode,
'billing_phone' => $request->phone,
'billing_total' => Cart::getTotal(),
'error' => $error,
]);
foreach (Cart::getContent() as $item)
{
OrderProduct::create([
'order_id' => $order->id,
'product_id' => $item->model->id,
'quantity' => $item->quantity,
]);
}
}
Thanks for concern.
You have to pass parameter through route as well:
Route::get('/success/{error}', 'RaveController#addToOrdersTables')->name('success');
And In addToOrdersTables method type hint the request using Request Like given below:
protected function addToOrdersTables(Request $request, $error)

Argument 1 passed to Darryldecode\Cart\Cart::Darryldecode\Cart\{closure}() must be an instance of Darryldecode\Cart\CartCondition

\Symfony\Component\Debug\Exception\FatalThrowableError
Argument 1 passed to Darryldecode\Cart\Cart::Darryldecode\Cart{closure}() must be an instance of Darryldecode\Cart\CartCondition, instance of Darryldecode\Cart\ItemCollection given
C:\wamp64\www\blog\vendor\darryldecode\cart\src\Darryldecode\Cart\Cart.php:589
I just did composer dumpautoload and I am getting this error after that when I go to http://localhost/ecom/public/cart . I am using Darryldecode Cart Library. Before dumpload the cart was working fine.
Cart Create function is working fine but not index
public function index()
{
$cartSubTotal = Cart::getSubTotal();
$condition = new \Darryldecode\Cart\CartCondition(array(
'name' => 'GST',
'type' => 'tax',
'target' => 'total',
'value' => '5%',
'attributes' => array( // attributes field is optional
'description' => 'Goods & Services Tax',
'more_data' => 'It is 5% of the total Value'
)
));
$cartTotal = Cart::getTotal();
$gst = $cartSubTotal * 0.05;
$datas = Cart::getContent();
//$product = Products::whereIn('id', $datas->pluck('id')->all())->get();
return view('cart.index', compact('datas','cartSubTotal','gst','cartTotal','condition'));
}
Ran into the same issue, using example code from Darryl.
CacheStorage is working fine, but switching to DatabaseStorage gave the error.
As I don't use conditions, this simple fix in DbStorage will do for me:
public function get($key)
{
$cart = $this->has($key);
if($cart)
{
if (str_contains($key,'conditions')) {
return $cart->conditions; // return empty array
}
else {
return new CartCollection($cart->cart_data);
}
}
else
{
return new CartCollection([]);
}
}

phpunit test in laravel doesn't show the real sesion table

When I use the browser, I see that the values in the session are correct because I use them to display data.
However, when I display a session table in phpunit, it only shows the value for one key:
Controller:
public function add(Book $book)
{
$cart = session('cart');
Book::addToCart($cart, $book);
return back();
}
Model:
public static function addToCart($cart, Book $book)
{
if (!$cart) {
self::createCart($book);
} else {
self::updateCart($cart, $book);
}
}
private static function createCart(Book $book)
{
$cart = [
$book->id => [
'id' => $book->id,
'title' => $book->title,
'quantity' => 1,
'image' => $book->image,
'price' => $book->price
]
];
session(['cart' => $cart]);
}
private static function updateCart($cart, Book $book)
{
if(isset($cart[$book->id])){
$cart[$book->id]['quantity']++;
session(['cart' => $cart]);
} else {
$cart[$book->id] = [
'id' => $book->id,
'title' => $book->title,
'quantity' => 1,
'image' => $book->image,
'price' => $book->price
];
session(['cart' => $cart]);
}
}
for example simple test (result in img)
use WithoutMiddleware;
/** #test */
public function cart_is_created()
{
$book = Book::first();
$this->get('/add-to-cart/' . $book)
->assertSessionHas('cart');
dd(session()->all());
}
Could you show us your tests? When using phpunit with Laravel there's some traits/classes that make sure you connect to a test database instead. This database is usually emptied before testing, which might explain you findings.

How to access a variable saved in the store method?

I'm using nested set comments (Kalnoy package) in my project and I'm stuck at creating children comments. I created two different method for both type of comments.
Saving root comments works fine:
public function storeComments(Request $request, Post $post)
{
$comment = Comment::create(
[
'body' => request('body'),
'user_id' => auth()->id(),
'post_id' => $post->id,
]
)->saveAsRoot();
return back();
}
However children comments are still saved as root comments.
public function storeNestedComments(Request $request, Comment $comment, Post $post)
{
$comment->children()->create(
[
'body' => request('body'),
'user_id' => auth()->id(),
'parent_id' => $comment->id,
'post_id' => $post->id,
]
);
return back();
}
This $comment variable in the second method is naturally null. How can I access the comment that was saved as root?
Update: saveAsRoot() logic
public function saveAsRoot()
{
if ($this->exists && $this->isRoot()) {
return $this->save();
}
return $this->makeRoot()->save();
}
This should do the trick:
public function storeNestedComments($parent_comment_id)
{
$parent = Comment::findOrFail($parent_comment_id);
Comment::create([
'body' => request('body'),
'user_id' => auth()->id(),
'parent_id' => $parent->id,
'post_id' => $parent->post_id
], $parent);
return back();
}
I corrected the way you are retrieving the parent commend, it does the same, but better written, plus it will throw a ModelNotFoundExceptionif the comment cannot be retrieved :)
#Amaury gave me a hint :)
I changed my route to include the root comment id
Route::post('/posts/{post}/{comment}/nestedcomments', 'CommentsController#storeNestedComments');
Passed that id to the method, and associated the child id with the parent.
public function storeNestedComments($parent_comment_id)
{
$comment = Comment::where('id', $parent_comment_id)->first();
$nestedComment = Comment::create(
[
'body' => request('body'),
'user_id' => auth()->id(),
'parent_id' => $parent_comment_id,
'post_id' => $comment->post_id,
]
);
$nestedComment->parent()->associate($comment)->save();
return back();
}

Cakephp AuthComponent: Login successfull despite wrong password

I'm running a 2.2.2 CakePHP Application, everything works as desired. Now I'm developing a Android App for it and therefore need to create the interfaces between those two apps. That's why I need to login users manually. So I created a whole new controller, the AndroidController, in order to bundle everything at one place. First thing to do would be the Login-Action. So I setup the following controller:
<?php
App::uses('AppController', 'Controller');
/**
* Android Controller
*
* #package app.Controller
*/
class AndroidController extends AppController {
public $components = array('RequestHandler','Auth');
public $uses = array('User');
public function beforeFilter() {
$this->Auth->allow();
}
public function login() {
//For testing purposes
$postarray = array('_method' => 'POST','data' => array('User' => array('email' => 'user#gmail.com', 'password' => 'THISisDEFINITELYaWRONGpassword')));
$id = $this->tryToGetUserID($postarray['data']['User']['email']);
if($id == 0){
//return Error json, unknown User
$this->set('result', array(
'tag' => 'login',
'success' => 0,
'error' => 1,
'error_msg' => 'Unknown User'
));
}else{
// if ($this->request->is('post')) {
$postarray['data']['User'] = array_merge($postarray['data']['User'], array('id' => $id));
$this->User->id = $id;
if ( $this->Auth->login($postarray['data']['User'])) {
// Login successfull
$this->User->saveField('lastlogin', date(DATE_ATOM));
$user = $this->User->find('all', array(
'recursive' => 0, //int
'conditions' => array('User.id' => $id)
));
$loggedInUser = array(
'tag' => 'login',
'success' => 1,
'error' => 0,
'uid' => '??',
'user' => array(
'name' => $user['0']['User']['forename'].' '.$user['0']['User']['surname'],
'email' => $user['0']['User']['email'],
'created_at' => $user['0']['User']['created'],
'updated_at' => $user['0']['User']['lastlogin']
)
);
$this->set('result', $loggedInUser);
} else {
// Login failed
$this->set('result', array(
'tag' => 'login',
'success' => 0,
'error' => 2,
'error_msg' => 'Incorrect password!'
));
}
// }
}
}
public function tryToGetUserID($email = null) {
$user = $this->User->find('list', array(
'conditions' => array('User.email' => $email)
));
if(!empty($user)){
return array_keys($user)['0'];
}else{
return 0;
}
}
}
You need to know that this method will be called as a POST request, but for testing purposes I manually created a post-array. In future I will use the $_POST array.
So, what happens: The Login with a registered user works, but it works every time! Even though the password is wrong or missing! The program never reaches the part in code with the "Login failed" comment.
Am I missing something here..?
Thank you!
If you take a closer look at the documentation you will notice that AuthComponent::login() will ...
In 2.x $this->Auth->login($this->request->data) will log the user in with whatever data is posted

Categories