Codeigniter always load in controller - templates

I`m from Russia, so sorry for bad English.
I want to load template in every page in controller.
For example (library parser in autoload),
class Blog extends CI_Controller {
$header = array(
'header' => 'Welcome to my blog!'
);
$this->parser->parse('header', $header);
function index() {
...
echo "My text";
}
header.php:
<h1>{header}</h1>
<script>
alert("Welcome!");
</script>
But I get an PHP error:
syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in line 6. Line 6:
$header = array(
How can I load header in every page? Thanks.

config.php:
$autoload['libraries'] = array('parser');
controller blog.php:
<?php
class Blog extends Controller {
function __construct()
{
parent::Controller();
$header = array('header' => 'Welcome to my blog!');
$this->parser->parse('header', $header);
}
function index()
{
echo "Мой текст";
}
}
?>
view header.php:
{header}
it works for me..

try calling the array with $this->header

Maybe it's a typing error but in the code from header.php you have typed {header} where I guess it should be {$header}

When loading class properties, like your $header above, php expects the property's visibility to be declared before the variable name. PHP5 has three visibility options: 'public', 'private', or 'protected'. I think this is why you are getting "unexpected T_VARIABLE". The difference between them are described at swik.net as:
To protect from accessibility pollution, PHP v5 introduces 3 prefixes for declaring class methods or variables: public, protected, and private.
Public methods and variables are accessible outside of the class. Protected are only accessible from inside of the class and inherited or parent classes. Private are only accessible from within the class itself.
try this: (I chose 'public' visibility, you can determine which is appropriate for your use)
public $header = array('header'=>'Welcome to my blog");
Next, I think you should call your parser in a constructor, rather than outside of a class method.
function _construct(){
parent::_construct();
$this->parser->parse('header',$this->header);
}
The constructor will be called every time the class is instantiated, loading your parser library method along with it.
Update:
Your comment suggests that the parser isn't working like you expect. I assume you have placed
$this->parser->parse('header,$this->header);
in the constructor function like I suggested. If that isn't working, create a function with the same name as your class and put the parser in there, that function will load every time the class is called, similar to the constructor, but let's see if that does the trick. I suggest taking the parser library out of auto load until you've resolved your problem, just to simplify things.
function blog(){
$this->load->library('parser');
$this->parser->parse('header',$this->header);
}

Related

How do I pass data to a component in Laravel Livewire?

class Counter extends Component
{
public $counter = 0;
public $name;
public function mount($name)
{
$this->name = $name;
}
public function render()
{
return view('livewire.counter');
}
}
I'm trying to pass the name variable as parameter to the livewire component call like so #livewire('counter', ['name' => $name]) but receiving "Undefined variable $name" error. I have been following the livewire docs too, still same error. what could possibly be wrong? Thank you.
If $name is not defined, then it is not a property available in the blade file where you are including Livewire. Since we don't know anything about your blade file or controllers, I can't give you a direct answer. However, you should check if $name is set anywhere and if so, if it's available in your blade view. This error is not related to Livewire. If you've simply copied the docs without checking the variables, you might as well set it right away:
#livewire('counter', ['name' => 'Gilles'])

How can be any service injected into WebTestCase subclass in Symfony?

Maybe I am missing something... doh, I think so, but could not find an answer to that.
WebTestCase generates this constructor sample:
public function __construct(?string $name = null, array $data = [], string $dataName = '')
{
parent::__construct($name, $data, $dataName);
}
Was trying to add my service as the first or last argument - Symfony throws an error:
Type error: Too few arguments to function Tests\AppBundle\Manager\ContactManagerTest::__construct(), 0 passed in /Library/WebServer/Documents/HEPT/vendor/bin/.phpunit/phpunit-5.7/src/Framework/TestSuite.php on line 568 and at least 1 expected in /Library/WebServer/Documents/HEPT/tests/AppBundle/Manager/ContactManagerTest.php:22
Should I somehow use container directly? Why is autowiring not working for WebTestCase classes if there is a bridge class?
WebTestCase are used in the context of PHPUnit (which has nothing to do with Symfony and its dependency injection).
They actually generate the kernel and its container, see this piece of code extracted from Symfony source code:
protected static function createClient(array $options = array(), array $server = array())
{
$kernel = static::bootKernel($options);
$client = $kernel->getContainer()->get('test.client');
$client->setServerParameters($server);
return $client;
}
This means that you can easily access the container like this:
$kernel = static::bootKernel($options);
$container = $kernel->getContainer();
Please note also that static::$kernel->getContainer() is available as soon as you created your client to make your test.

why Route::currentRouteName() not working in laravel 5 inside a controller method

why Route::currentRouteName() not working in laravel 5 inside a controller method , but when i provide '\' before class name works . I need to know the reason why adding back slash before class works in this scenario. May be it is some oops scenerio , which i don't know . Please guide
$name = \Route::currentRouteName();
You need to understand how PHP Namespaces works.
Route is not within the controller namespace hence you need to provide the full namespace to Route class for it to be loaded. Route is in root namespace hence you reference it with \Route just like you would do in directory structures.
Assuming your controller looks like this:
<?php namespace App\Http\Controller
class HomeController extends Controller{
public function index(){
//Doing this will throw a Not Found exception because route is
//not inside `App\Http\Controller` namespace
$name = Route::currentRouteName();
//However if you specify the correct namespace it works like so:
$name = \Route::currentRouteName();
}
}
You can read more about namespace here:
http://php.net/manual/en/language.namespaces.php
You can use
\Request::route()->getName()
or
\Route::getCurrentRoute()->getPath();

Joomla 2.5 Virtuemart hook method in a custom plugin wont fire

I managed to create this simple plugin that I need it to fire a method when ever an item is added to the cart and a method to fire when a checkout has happend. But those methods wont fire in anyway. I also seen another plugin named stockable in virtuemart that uses plgVmOnAddToCart and it fires there correctly. But in my plugins class it wont fire at all.
Here is my code in my plugin, what can I do to make it work? thank you
<?php
defined('_JEXEC') or die( 'Direct Access to ' . basename( __FILE__ ) . ' is not allowed.' ) ;
if (!class_exists('vmCustomPlugin')) require(JPATH_VM_PLUGINS . DS . 'vmcustomplugin.php');
class plgVmAftercheckout extends vmCustomPlugin {
private $stockhandle = 0;
function __construct(& $subject, $config) {
parent::__construct($subject, $config);
$varsToPush = array(
'selectname1'=>array('','char'),'selectname2'=>array('','char'),'selectname3'=>array('','char'),'selectname4'=>array('','char'),
'selectoptions1'=>array('','char'),'selectoptions2'=>array('','char'),'selectoptions3'=>array('','char'),'selectoptions4'=>array('','char')
);
$this->setConfigParameterable('custom_params',$varsToPush);
}
public function plgVmOnAddToCart(&$product){
echo "plgVmOnAddToCart fired";
die();
}
public function plgVmOnUserInvoice(){
echo "plgVmOnUserInvoice fired";
die();
}
}
?>
Ok I found the solution. The problem was the class name. In the joomla 1.5 documentation
http://docs.joomla.org/Creating_a_Plugin_for_Joomla_1.5
it mentions that the class name must follow this rule:
class plg extends JPlugin
But this is not mentiond in any joomla 2.5 documentation, since it is probably considered " an already known rule ".
So my solution was to change the class name from
class plgVmAftercheckout extends vmCustomPlugin {
to
class plgVmCustomAftercheckout extends vmCustomPlugin {
"Custom" because the plugin belongs to a specific group called Custom. So we need to mention the group name in order to make those hook methods observe the events.

Symfony2: global variables in php templating engine

There is a cookbook for adding globals to the twig templating engine, but it doesn't get into doing the same thing for the php engine. How would I do this?
So I might have something like:
# config.yml
someSortOfReferenceToThePHPEngineInstance:
calls:
- [ addGlobals, ["foo", "bar"] ]
- [ addGlobals, ["myService", "#myService"] ]
And then access those like:
// templateName.contentType.php
<?
echo $foo; // echos "bar"
echo $myService->myMethod($foo); // echos the result of modifying "bar" with "myMethod" method of "myService" service
I could not find any documention on this for the PHP engine...
What does work however is:
Config:
//config.yml
parameters:
hello: "YO!"
PHP Template:
// index.html.php
<?php
print $view->container->parameters['hello'];
This does not fit as nicely as the twig convention... Maybe there is better way - I have not debugged any further...
Here are a couple of options:
If you create a base controller that all others inherit from, you can override symfony's render function and add keys to the parameters argument, like:
public function render($view, array $parameters = array(), Response $response = null){
if(!array_key_exists("bar", $parameters){
$parameters["foo"] = $this->get("foo");
}
if(!array_key_exists("bar", $parameters){
$parameters["bar"] = $this->get("bar");
}
return parent::render($view, $parameters, $response);
}
This is the only way I see to modify the "global" variables "globally", though they'll not be available in any views rendered by controllers you don't create (of course, those'll likely be done in Twig anyway and you can use the normal twig means of adding functionality).
The PHP rendering engine has what're called "helpers", which you can access via array keys of $view, like:
$view["foo"]->doSomething();
We created a class for easily making services into helpers:
use Symfony\Component\Templating\Helper\Helper as BaseHelper;
class Helper extends BaseHelper{
protected $name;
public $service;
public function __construct($name, $service){
$this->name = $name;
$this->service = $service;
}
public function __get($name){
if(isset($this->service->$name)){
return $this->service->$name;
}
}
public function __call($name, $arguments){
if(method_exists($this->service, $name)){
return call_user_func_array(array($this->service,$name), $arguments);
}
}
public function getName(){
return $this->name;
}
}
Then in our configuration under the services we'd add:
helper.foo:
class: %helper.class%
arguments:
name: "foo"
helper: "#foo"
tags:
- { name: templating.helper, alias: foo }
This would theoretically be available then to any view files, even those with controllers you don't have control of.
I had a very same problem. For some reason this feature is only available for Twig templating with TwigBundle. Both Twig and PHP templating engines provide possibility to define global variables, but only Twig engine has configuration for that. For me the only real way to achieve that is something you proposed in the question post - to define method calls (and this is the way Twig globals are registered).
Problem is, that with DI extensions you can't access service definition from outside your extension, so you can't add these calls from your DI extension. The way for me was to do that with DI compiler pass.
But I'm also developer of ChillDevViewHelpersBundle and since I was facing this problem in most of my projects I decided to implement it there for common use and you can use 0.1.8 release for this feature.