I try to display Google Map which is display an address from database(I have address column in DB table). For this I made a blade, and bind to route and path it with controller. I am having display issue.
My step is right below. I used this google API.
https://github.com/farhanwazir/laravelgooglemaps
And set the route:
Route::get('/show', 'PagesController#map');
Set the controller:
public function map(){
$config['center'] = allestates::where('address')->get();
$config['zoom'] = '10';
$config['map_width'] = '300px';
$config['scrollwheel'] = false;
GMaps::initialize($config);
$map = GMaps::create_map();
return view('pages.show',[ 'map' => $map]);
}
And in my blade. This is how I am calling it in body tag.
{{$map['html']}}
But getting this error.
Non-static method FarhanWazir\GoogleMaps\GMaps::initialize() should
not be called statically
Any idea what the problem is?
This code works for me:
$gmap = new GMaps();
$gmap->initialize($config);
$map = $gmap->create_map();
return view('your_view', compact('map'));
Notice that $gmap->create_map() is not a static calling.
Try to do it like this
return view('pages.show',[ 'map' => $map]);
return view(pages.show)->with(['map'=> $map]);
Also check if the value is not empty
Goodluck
Related
I working in Laravel project and I can't call static method across variable
For example:
$objName = 'User';
$objName::get();
On this way I get error.
$objName = 'User';
Is a string, for using the get() method $objName should be an object, for example:
$objName = User::all()->first(); // this will return an object
Ok I use
User::all();
But I want get parametar from URL for example www.example.com/User, www.example.com/Articles -> User and Article is parametar in URL (this is Laravel web route) and call static method. When I wrtie first URL than call User object if I write first URL than call Article object.
www.example.com/User
$param= 'User';
$param::all();
www.example.com/Article
$param= 'Article';
$param::all()
I find solution!
This solution in Laravel:
$data = call_user_func( array('\App\\'.$param , 'all'));
but if you want use in plain PHP then is:
$data = call_user_func( array($param , 'all'));
This will call Object and Method.
If you want to send arg in methond thent is:
$data = call_user_func( array('\App\\'.$param , 'all'), $arg); /*For Laravel*/
$data = call_user_func( array($param , 'all'), $arg); /*For plain PHP*/
Is it possible get the value of a route placeholder within a Slim container? I know I can access the placeholder by adding a third parameter to the request but I'd like to have it injected so I'm not assigning it on each request.
I've tried $app->getContainer('router') but I can't seem to find a method to actually pull the placeholder value.
Example:
$app = new Slim\App;
$c = $app->getContainer();
$c['Controller'] = function() {
$userId = // how do I get the route placeholder userId?
return new Controller($userId);
};
$app->get('/user/{userId}','Controller:getUserId');
class Controller {
public function __construct($userId) {
$this->userId = $userId;
}
public function getUserId($request,$response) {
return $response->withJson($this->userId);
}
}
Without some 'hacky' things this will not work because we have no access on the request object build by slim, while the controller get constructed. So I recommend you to just use the 3rd parameter and get your userid from there.
The 'hacky' thing would be todo the same, what slim does when you execute $app->run(), but if you really want todo this, here you'll go:
$c['Controller'] = function($c) {
$routeInfo = $c['router']->dispatch($c['request']);
$args = $routeInfo[2];
$userId = $args['userId'];
return new Controller($userId);
};
Note: slim3 also urldecoded this values so may do this as well urldecode($args['userId']) Source
create a container wrapper and a maincontroller then extend all your controller from your maincontroller, then you have access to the container.
here is how i solved this problem:
https://gist.github.com/boscho87/d5834ac1ba42aa3da02e905aa346ee30
I want to give a 3rd party PHP application access to Yii2 user data (HumHub) and have tried this:
function getUserId() {
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$yiiConfig = require('../protected/config/common.php');
(new humhub\components\Application($yiiConfig));
$user = Yii::$app->user->identity;
return $user;
}
This does not work. There are no errors up until new humhub\components\Application($yiiConfig) but then the 3rd party app breaks with no error thrown and the function does not return anything.
I did find this solution which does not work.
Is there are reason this does not work or is there an alternate solution to getting Yii2 user data properly?
This is how to do it in HumHub V1.0
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$config = yii\helpers\ArrayHelper::merge(
require('../protected/humhub/config/common.php'),
require('../protected/humhub/config/web.php'),
(is_readable('../protected/config/dynamic.php')) ? require('../protected/config/dynamic.php') : [],
require('../protected/config/common.php'),
require('../protected/config/web.php')
);
new yii\web\Application($config); // No 'run()' invocation!
Now I can get $user object:
$user = Yii::$app->user->identity;
Indeed an error should be thrown, but, the error settings of your PHP may be overridden or set to not display errors.
You call undefined object Yii::$app->user->identity. The reason, from documentation because you have not initialized the Yii object. So your code should be as follows:
function getUserId() {
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$yiiConfig = require('../protected/config/common.php');
(new humhub\components\Application($yiiConfig)); // try to comment this line too if it does not work
// Add The Following Line
new yii\web\Application($yiiConfig); // Do NOT call run() here
$user = Yii::$app->user->identity;
return $user;
}
I am returning Json response from zend controller.
This is my code
Controller Code
public function get()
{
$result = //calling other function and getting response in array
print("value in controller before calling toJsonModel");
print_r($result);
$temp = $this->toJsonModel($result);
var_dump($temp);
return $temp;
}
toJsonModel function
public function toJsonModel($data = array())
{
$data['requestId'] = $this->getHeader('RequestId');
print("Value just before returning to controller");
print_r($data);
return new JsonModel($data);
}
Response
First and second print displays correct values. But after getting values from toJsonModel when I try to displays wrong values and adds some other values like "captureTo", "terminate" and "children" as protected.
I don't know why it's giving me wrong message.
Can any one guide me to right path and tell me what the problem is ?
Thanks in advance.
EDIT
I am adding new screenshot to display full message.
ERROR :
{"name":"Email Not Found","message":"No account found with this email
address.","code":404,"requestId":"12","reason":"error-controller-cannot-dispatch","display_exceptions":true,"controller":"RSMobile\Controller\ForgotPassword","controller_class":null}
I finally solved this problem.
I was throwing some error from controller whenever there is a problem. I used to throw 404 when email not found. That was causing this error.
At first i thought that this is a problem related to JSON model. But thanks to #Bilal who helped me in figuring out the problem.
When I throw 404 from controller it by default appends all these data at the end. But when i throw any error other than 404, it works.
This is because you are returning an object of JsonModel from method toJsonModel.
return new JsonModel($data);
So, every object has some properties that you can use to manipulate data. See the docs of Zend\View\Model\JsonModel here
So actually the values are not wrong but there is a way to access properties of an object.
Update
Attaching dispatch event
public function onBootstrap($e)
{
// some stuff
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach("dispatch", function($e) {
echo "Dispatch!";
});
// some stuff
}
I have the following function:
public function make_order($id = null){
if($this->request->is('post')){
if(isset($id)){
$single_product = $this->Product->find('first', array('Product.id' => $id));
$this->placeOrder($single_product);
}else{
$product_array = $_SESSION['basket'];
foreach($product_array as $product){
$this->placeOrder($product);
}
}
}
}
private function placeOrder($product){
$order_array = array();
$order_array['Order']['Product_id'] = $product['Product']['id'];
$order_array['Order']['lejer_id'] = $this->userid;
$order_array['Order']['udlejer_id'] = $product['users_id'];
$this->Order->add($order_array);
}
Now these two function are not "connected" to a view but i still need to call them from within another view
For this ive tried the following:
<?php echo $this->Html->link(__('Bestil'), array('action' => 'make_order')); ?>
However this throws an error saying it couldnt find the view matching make_order and for good reason ( i havnt created one and i do not intend to create one)
My question is how do i call and execute this function from within my view?
At the end of your make_order function, you'll either need to:
a) specify a view file to render, or
b) redirect to a different controller and / or action, that does have a view file to render.
a) would look like this:
$this->render('some_other_view_file');
b) might look like this (note: setting the flash message is optional)
$this->Session->setFlash(__('Your order was placed'));
$this->redirect(array('controller' => 'some_controller', 'action' => 'some_action'));
You can turn auto-rendering off by setting $this->autoRender = false; in your controller's action (make_order() in this case). This way you don't need a view file, and you can output whatever you need.
The problem is that nothing will be rendered on the screen. Therefore, my advice is to have your "link" simply call a controller::action via AJAX. If that's not possible in your situation, then you'll have to either render a view in your make_order() method, or redirect to an action that will render a view.