Symfony use a personal bundle in my main bundle - bundle

to let you understand what I mean, I will start with an example.
Imagine I have a bundle (MainBundle) which contains my whole besite.
And I want to create a new bundle (CommentBundle) which I will use in my MainBundle to display the comments in each articles.
How can I call the controller of CommentBundle which displays the wanted comments in my MainBundle ?
I mean, it's quite difficult to imagine how that would be possible.
But I heard about the face that bundles were created for this purpose, so I wanted to learn to do it write.
Can someone give me some advices ?
Thank you
Edit :
I did this, but it returned me "Call to a member function has() on null"
public function indexAction()
{
$commentController = new \CommentBundle\Controller\DefaultController();
$comments = $commentController->indexAction();
return new Response($comments);
}

Related

Laravel: How to use function of relation

I'm facing a situation where a I have to call a function from the controller of a relation instance. For better explanation I will write an example below
I have a Article controller in which I have a preview() function.
A User can have multiple Article.
Let's say that the preview() function parse a text and replace special pieces of text by the user's name.
So my function will looks like this
//In ArticleController
public function preview(Article $article , User $user){
return str_replace("username", $user->name , $article->text);
}
But for a specific situation I want to display a preview of the article when I list all the users
So in UserController
public function index(){
foreach( User::all() as $user){
echo $user->articles[0]->preview( ... );
}
}
Obviously this piece of code will not work.
But I'm more looking of the way to proceed when I face this kind demand.
Should I create a Repository? Use this preview() function somewhere else? Or Is it just a bad practice to do that? What's the best approach or way of thinking when we face this?
Or maybe I'm just missing something important in Laravel's ORM. :/
I assume Article is a model. So you have to add hasMany relation to User (user has many articles). Inside article you have to add preview function. In this case you will be able to find $user->article (or user->articles) and run ->preview function. This is the easiest solution I guess.
You can also add custom attribute like getPreviewAttribute and append it to article model. This way you would have $user->article->preview.

laravel newbie- syntax for making/setting variable in view with a function in the controller

I've started my first project in laravel (following along with the beginner laracasts series). I've successfully made a 'joggers' scope and get_joggers() function in the controller to pass that set of data to a view- great!
So now- I have a view where I pass all the people, and a partial to show different subsets of them, like so:
#include ('partials._people_list', ['person_group' => $joggers]);
My question: On a view page, what's the syntax declare/set a variable to the results of a function in the controller? I would imagine something like this, but since a view page wants HTML I'd at least have to put it in some php tags or something (though there's probably a better way):
$person_group = action('PeopleController#get_joggers');
This seems so painfully basic, but the first 27 laracasts didn't get there and I've been hunting around for while. Thanks in advance for any help!
Update! I talked to a buddy at work, and the answer was to make/set $joggers in the PeopleController, and then pass it all along like so:
public function index()
{
$people = Person::latest('created_at')
->displayable()
->orderBy('last', 'asc', 'first', 'asc')
->get();
$joggers= Person::joggers('created_at')->get();
return view('person.index', compact('people', 'joggers'));
I've returned different types of data before, and it hadn't occurred to me that I could send two different collections of people. Mystery solved :)

Laravel created_by/modified_by relations

I was trying to get this working in a typical belongsTo relation. However it keeps saying that the column is not set in the model, even if looking in the actual database it is there.
I have tried to look at the source code as well as try many approaches to bypass this issue, however nothing seems to do anything.
public function modifiedBy()
{
return $this->belongsTo('\Modules\Users\Model\User', 'modified_by');
}
public function createdBy()
{
return $this->belongsTo('\Modules\Users\Model\User', 'created_by');
}
This is the code inside the model, I use PSR-0 to define modules, better splitting up logic (no issues with that) but using this it would give an error of
Undefined property: \Modules\Module\Model\CurrentModel::$modified_by
This is coming from a seed to push some initial info into the database.
$user = Sentinel::findById(1);
$model = new CurrentModel;
$model->modifiedBy()->associate($user);
$model->save();
This is basically how it goes together, I have tried for some time to figure out what is wrong but I am calling blanks. Any ideas?
Found out a solution. Not a fix though but I would consider this an issue with laravel so I may look into adding it as a bug report (although this could be fixed in laravel 5?).
Basically with modified_by I need to define the column it is using and not let laravel automatically generate it (in order to do this "cleanly"). However the "bug" (only calling it a bug as currently I can only see this as an unintended problem) makes it so you cannot define the column it will be using, you have to let laravel decide it for you.
So I changed the functions to look like this:
public function modifiedby()
{
return $this->belongsTo('\Modules\Users\Model\User');
}
This makes laravel assume the column is modifiedby_id, but by changing my migrations to reflect that there was no more error.

Joomla - call function model within the model

In a public function of my model I call
$user_type=$this->get_user_type();
In the same model I have
private function get_user_type()
{
$user_type='asd';
$asd_groups = (int)$config->get('asd_groups');
$ver_groups = (int)$config->get('ver_groups');
jimport( 'joomla.user.helper' );
$user_groups=JUserHelper::getUserGroups($user->id);
if(in_array($asd_groups,$user_groups)){
$user_type='asd';
}
if(in_array($ver_groups,$user_groups)){
$user_type='ver';
}
return $user_type;
}
The site give me a white page, if I comment the calling line "$this->get_user_type();" then it works...
I really don't understand what is wrong here.
There is not enough information or code here to help you… for example where is $config coming from and what is it? What version of Joomla is this on?
If $config is not defined as a global then that may be the source of the problem depending on your PHP setup.
Things you can do to help yourself find the problem, in Joomla's Global Configuration.
Set Error Messages to "Development" in Joomla (you are using a development site and not a live website right?)
Turn on Joomla's DEBUG mode
Then update your question with details of error messages, Joomla version and where this code is running (you say your model) and where $config is coming from.
Oh sure!
I have missed the two configuration variable when i moved the code from inside a function in a dedicated function.
I copied these two lines on the first row of the function and now it works!
$config = JComponentHelper::getParams(S_APP_NAME);
$user = JFactory::getUser ();

Making improvements to code

I have been working on a magento module for sometime now (not because it is a large module, its my first and I really struggled with it). I now have it working, which I was really pleased with at first, but now I would like to improve it by increasing the reusability of the code.
While making my module I have looked at other modules to learn from them and have seen that many actions are only a line or two and I have tried keeping in with the skinny controller approach but failed, my example is as follows:
I have a function that get a users details that they have inputted into a custom form
protected function _setPostData()
{
$this->_salutation = $this->getRequest()->getPost('salutation');
$this->_f_name = $this->getRequest()->getPost('f_name');
$this->_l_name = $this->getRequest()->getPost('l_name');
$this->_email = $this->getRequest()->getPost('email');
$this->_emailAddressCheck = $this->getRequest()->getPost('emailAddressCheck');
$this->_gender = $this->getRequest()->getPost('gender');
$this->_country = $this->getRequest()->getPost('country');
$this->_pref_lang = $this->getRequest()->getPost('pref_lang');
}
I feel that this is not the correct way to do it and that there is a better way of achieving the same goal, as you can see this function gets the posts data and assigns it to attributes that i've set at the start of the class. I have several other examples that are very similar to the above and if someone could please offer some guidance on this one I am sure I will be able to work out the others
This example is held within the index action, should I put it in a helper as once it created correctly i am sure there will be a few occasions that I will be able to use it again?
you should put all the post data on an array and use it from there
$dataArray=$this->getRequest()->getPost();
$this->_salutation = $dataArray['salutation'];
$this->_f_name = $dataArray['f_name'];
$this->_l_name = $dataArray['l_name'];

Resources