I was following the example to implement custom filters in Joomla 2.5 admin component.
But I am getting error at models populateState method:
Call to undefined method
somecompModelsomecomp::getUserStateFromRequest().
$app = JFactory::getApplication('administrator');
// Load the filter state.
$search = $this->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
Error disappears if I call getUserStateFromRequest using $app:
$app->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
So whats the problem? In default Joomla components I've seen that it use the same approach and it works. Maybe I miss something in my model class?
Any ideas?
This is happened because $app is an object of your application class. As you defined it in your code.
$app = JFactory::getApplication('administrator');
and getUserStateFromRequest method is defind in that Application class.so you have to use it like this if you want to access this method.
$app->getUserStateFromRequest($this->context.'.filter.search', 'filter_search');
And for your information $this variable is your local object.
Related
I am trying to implement Policies in my project and I have a custom method askFriend that I want to add to my UserRelationPolicy.
So I implemented in my UserRelationPolicy the askFriend method but when trying to call it from the UserRelationPolicy#askFriend I asked myself how to call it from this method.
Something like $this->authorize('askFriend', $friend); but it was not working, kind of ignoring it at all. So I searched further in the documentation and found that I could bind with a Gate method the specific method in the UserRelationPolicy to a resource name like this :
Gate::resource('userrelation', 'UserRelationPolicy', [
'userrelation.askfriendrelation' => 'askFriendRelation'
]);
You can find the representation here : Documentation Writing Gate
When I try to execute this code I get the following error :
Call to undefined method Illuminate\Auth\Access\Gate::resource()
And nothing more. The Resource method doesn't seem to exist at all. After many search, trying to include every Gate in the header. Trying to call it staticly or with an instance. Nothing work and the method is nowhere near to be found...
Is it something forgotten ? How can I call a custom method from a controller in a policy class ?
Are you sure you are using 5.4? The method Gate::resource was implemented only in 5.4.
If you are using any version behind you will have to use the Gate::define.
Set the Gate abilities in the App\Providers\AuthServiceProvider like this:
Gate::define('userrelation.askfriendrelation', 'UserRelationPolicy#askFriend');
I'm playing with Silex microframework to build a very simple app.
The Silex documentation briefly illustrates how to keep your code organised using controller as class and I've also found this useful article talking about the same practice:
https://igor.io/2012/11/09/scaling-silex.html
but still can't solve my problem
The issue:
in my app.php I'm using
$app->get('/{artist}', 'MyNamespace\\MyController::getAlbum');
This is working. MyController is a class correctly loaded through composer using psr-4.
At the moment the return method of getAlbum($artist) is return $player;
What I'd like to do instead, is returning a twig view from getAlbum, something like:
return $app['twig']->render('player.twig', $player);
To do so, what I've tried to do in my custom class/controller is:
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
[...]
public function getAlbum(Request $request, Application $app, $artist)
but this is generating the following error when I try to access the routed pages:
ReflectionException in ControllerResolver.php line 43:
Class MyNamespace\Request does not exist
Whic made me think that there's a namespace conflict between myNamespace and the Silex namespaces?!
What am I doing wrong?
Is this the right way to make $app visible in my custom controller in order to use return $app['twig']... ?
Thank you in advance!
EDIT
After several other tries still didn't get to the point (replies still welcome!) but I've found a workaround solution that could be useful to anyone will incur in a similar issue. Directly in my app.php I added this
$app->get('/{artist}', function (Silex\Application $app, $artist) use ($app)
{
$object = new MyNamespace\MyController();
$player = $object->getAlbum($artist);
return $app['twig']->render('player.twig',
array(
//passing my custom method return to twig
'player' => $player,));
});
then in my player.twig I added:
{{player | raw}}
And this basically means that I still need an anonymous function to get use of my custom method which is a working solution I'm not really happy with because:
I'm using 2 functions for 1 purpose.
The return value of getAlbum is dependent from the use of "raw" in twig.
SOLVED
The workflow described works fine. It was a distraction error: I've placed the namespace of my custom class after use Symfony\Component\HttpFoundation\Request;
namespace declaration in PHP needs always to be at the top of the file, Silex wasn't able to injects $app and $request for this reason.
I'm creating a custom helper in codeigniter. There is an instance where I check if certain parameter is passed to view.
In view, i can get all the passed variables by using this function:
$this->_ci_cached_vars
but it returns blank when used in the custom helper.
Is there any equivalent function of this that can be used in the helper?
Thanks in advance.
_ci_cached_vars is a property of the Loader class. So something like this should work (untested):
$CI =& get_instance();
$vars = $CI->load->_ci_cached_vars;
You can use $GLOBALS['CI']->load->get_var('your_key_here') tested in CI 2.1.2
I'm not sure if older versions of CodeIgniter support this, but in the v3 release the Loader class has a public method get_vars() that allows you to read the value of _ci_cached_vars.
Although this question is very old it's the first hit on Google that I found while searching for this problem. I hope this post helps someone out who follows the same path on Google as I did! :)
I have a behavior which enables segregation of user data based on the user id stored in the session. In CakePHP 1.3 you could do this:
App::import('Component', 'Session');
$session = new SessionComponent();
$session->read('Auth.User.id');
But in CakePHP 2, you can't instantiate a component like that in a behavior because the Component __construct requires the Controller's ComponentCollection as a parameter.
Is it possible to access a session variable inside a behavior in CakePHP 2? What's the best way to do it?
If you look at the SessionComponent code, you will see that it is only a wrapper for the CakeSession class.
So you can do the following:
App::uses('CakeSession', 'Model/Datasource');
$user_id = CakeSession::read('Auth.User.id');
In CakePHP 2.0 you can also simply call the Session-methods via the static CakeSession::method() without having to load anything... ;-)
i'm wondering how i can manually start execute a controller action of my MVC application. My goal is to integrate the html output of /myController/myAction into another php application (typo3) using a simple include. I thought of manually instantiating the controller, the view and the layout, to bypass the dispatcher. Sadly i can't get it working. My current approach looks like this:
// standard bootstrap ... setting up autoloader, database etc.
$layout = new Zend_Layout();
$layout->setLayoutPath(('/application/default/layouts'));
$layout->setLayout('main');
$layout->setContentKey('content');
$view = new Zend_View();
$controller = new IndexController(new Zend_Controller_Request_Http($currenUrl), new Zend_Controller_Response_Http());
$controller->view = $view;
$controller->init();
$controller->hinweisAction();
$layout->content = $view->render();
echo $layout->render();
Instantiating the layout is no problem, but it becomes complicated when creating the controller. Setting the view instance after calling the constructor does not work, because the view is needed already during the instantiation.
What would be the "right" way for such a scenario? Maybe implement a simple user defined dispatcher which uses predefined controller and action names from me?
Greets
Georg Wächter
If you're using Zend_Application, all you need to do is something like this:
$application->bootstrap();
$request = new Zend_Controller_Request_Http();
// Set some parameters for request possibly
$controller = $controller = new IndexController($request, new Zend_Controller_Response_Http());
$controller->dispatch('hinweisAction');
Zend_Application will take care of setting up your view for you. Calling dispatch will take care of Action Helpers, particularly the ViewRenderer which does all the dirty work for you.
I'd suggest you looking at Zend_Test_PHPUnit_ControllerTestCase source code. It does exactly what you need to run tests against the content generated by a controller. Specifically, read the bootstrap() and dispatch() functions. May be you can just copy that verbatim.
Try:
$layout->content =
$controller->view->render();
$view is referring to the local instance of $view. Once you assign it to $controller->view, any work that $controller->init() and $controller->hinweisAction() does on it will affect $controller->view, not the local $view object.