Codeigniter gmap adding markers in view rather than controller - php

Trying to list a bunch of addresses with gmap.
Code in controller has this initialization steps:
$this->load->library('GMap');
$this->gmap->GoogleMapAPI();
$this->gmap->setMapType('map');
Was using the addMarkerByAddress in the same function as below and it was working:
$this->gmap->addMarkerByAddress($address,$ordername, $ordertitle, $tooltip, $icon_leaf);
When I try the same code in the view file it fails. I understand how to pass data to the views for display purposes (using the $data[] variable) but how do I get the gmap instance from the main file so it can be referenced in the view?

You can pass the object through
$data["gmap"] -> $this->gmap
Or you can create a Helper with the function/s you need, when you load the helper in the controller, you can access it without problem in the view.
Your view must not access controller's methods/variables/objects.

Related

How to pass data from one controller to other controller in codeigniter

I'm trying to code the system in that things are separated using "userId". That means, products or services are separated on the basis that user who add them in the system.
For that I'm trying to pass the userId from user controller to service and product controller. But I got an error that undefined variable userID.
$data['id'] = $this->session->userdata('user_id');
pass this in create() function of department controller.
i'm loading user model in __construct() function also.
Thanks in advance.
You can do it using two ways.
1) By Helper - simple create a custom helper file and create a function inside helper file and inside function add get userid code and then using function name you can access this userid anywhere in whole project.
2) Using By Require Controller Class -- if you want to access user id in another controller so i will explain with you by example.
Ist Controller - Test.php
here i defined a variable as a public and this variable contains userid value.
2nd Controller - Test2.php
here we will require Test.php file
require('Test.php file path');
and then create object and then we can access Test.php file variables and methods inside Test2.php file.
But i will suggest you first way it`s very easy way and multiple advantages.
Hope it will work for you.

Laravel - How to pass data to include

So essentially all of my views are using the header.blade.php since I am including it in my master layout. I need to pass data to the header on every single view. Is there a way to pass data just to the include rather than passing the data for the header in each view?
You don't need to do that, but you can:
All variables that are available to the parent view will be made
available to the included view. Even though the included view will
inherit all data available in the parent view, you may also pass an
array of extra data to the included view:
#include('view.name', ['some' => 'data'])
One option if you're trying to send data only to the included view is to use a view composer. They will fire even in the case of trying to prepare a view for #include
view()->composer('header', function($view) {
$view->with('data', 'some data');
});
actually the very best and faster method of sharing data to all views could be just using the
AppServiceProvider
instead of Jeff's answer you can use the share method instead of the composer method and achieve your goal faster.
Just pass the data you want in the boot method of the AppServiceProvider like following
public function boot()
{
View::share('key', 'value');
}
for more check this

Laravel 5: Controller Inter-Communication and Instantiation

In the past I've used a proprietary framework that kinda followed an Objective-C like View Controller scheme.
Basically I was able to in one controller instantiate another and pass it some values like an array of products and then inject the reference to the controller to my view and render it whenever I wanted by issuing: $controllerReference->render();
This could be useful in many cases, eg. if I had a controller responsible for rendering a catalog, I would just pass it an array with all the items I would like to see and it would take take of pagination and displaying the items by itself...
Example:
At \UI\Homepage\Controller.php (the controller responsible for the homepage):
// Instantiate a ProductDisplay Controller
$controllRef = new \UI\ProductDisplay\Controller();
$controllRef->setProducts( ... ); // Inject the product array
// Load the current view for this controller and pass it a reference for the ProductDisplay controller
$this->loadSelfView(["controllRef" => $controllRef]);
At \UI\Homepage\View.php (the view loaded before):
// some html of the view
$controllRef->render(); // Render the ProductDisplay view here!
// some other html
How should this functionality be accomplished in Laravel? From what I've been reading Laravel tries to avoid this kind of actions, why? What are the workarounds?
Thank you.
Here is how I will do this, it only work if the called controller method return a View object like return view('home');):
// Get the controller class from the IOC container
$myController= \App::make(MyController::class);
// Execute the controller method (which return a View object)
$view = $myController->myControllerMethod($someParams);
// Return the View html content
$html = $view->render();
you can use the Controller.php class which is extended by all other controller to make a generic method in it to:
Get a controller instance
Call the right method with x parameters
Return the rendered content if it's a view class OR throw an exception (for exemple)
In recent versions of Laravel, it's possible to use Blade's #inject directive. It seems its main purpose is to inject services but I successfully injected controller actions.
Here is a snippet on what you have to do:
#inject('productController', 'App\Http\Controllers\ProductController')
{!! $productController->index() !!}
Just remember: since you're calling the controller method directly, and not through the router, you'll have to pass all required params. If that action requires a request instance, you may call it this way:
{!! $productController->index(request()) !!}
Probably the view returned by the called action contains HTML code. It's important to wrap the method call within {!! and !!}. Using regular {{ }} tags will cause the HTML code be escaped by the template engine.

Removing controller name and function namein URL in Codeigniter CMS

I am building a cms in codeigniter and i want to remove controller name and function name form the url and just print the alias.
I will be having two controllers one for static pages and other for blog posts with categories.
Please help, Suggestions reagrding modification of two controllers are also welcome.
You will need to override the default 404 controller in application/config/routes.php.
$route['404_override'] = 'content';
Any request that can't be mapped to a controller will be passed to the application/controllers/content.php controller
Your Content controller, or whatever you decide to call it, will parse the uri [$this->uri->segment(1)] and check for a matching reference in your CMS database.
If there is no match in the database, then you can look for a static view in the views folder and load it.
if(is_file(FCPATH.'views/'.$this->uri->segment(1).'.php')) {
$this->load->view($controller,$this->data);
}
If no static view is found, and there is no matching content in the db, call the show_404() function.
Using this method, you will keep the default CI functionality of uri mapping, so at any time, you can add controllers as you normally would and the app will perform like a vanilla CI install.

CodeIgniter: some doubts about HMVC and Views

I've just discovered HMVC Modular Extension for CodeIgniter https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home and it seems perfect for my needs but I have some question.
Let's say I have two controllers:
Site which is the main controller and is used to show site's pages and may call methods of Users controller for example to show a form
User controller is used to authenticate users, to show login/sign up forms...
Now I have these questions:
If the user access the User controller directly (mysite.com/user/method) I want to show a full page while if i load a method of User from within the Site controller I want to show only a form (for example), is this possible?
What happens to view of a module loaded from another module: is the view shown automatically or i need to show it manually and how does the view behave?
If you method is being called via Modules::run()
There is a third optional parameter lets you change the behavior of
the function so that it returns data as a string rather than sending
it to your browser.
Eg:
//put underscore in front to prevent uri access to this method.
public function _module1()
{
$this->load->view('partial_view', array('some data'=>'some data'), TRUE)
}
call it inside your SITE view easily
Modules::run('User/_module1')
// should show whatever is in partial_view ie: a form
//an alternative is to pass in any params if the method requires them
Modules::run('User/_module1', $param)

Categories