When I study skeleton application of Zend Framework 2, I want to add a label at upper-right of page to show the UserName who have logged in. But, I am confused at the code of the navigation bar that was defined in layout.pthml,how can controller communicate with the layout.phtml to modify it?
Thanks in advance!
Furthermore, I want a login form upper-right of page when user not logged in using a helper.But i don't know how to add a form using helper,what should i do?
From a controller you can use the controller plugin called “Layout” to set a variable:
$this->layout()->username = “some value”;
Then in layout.phtml you should be able to do:
<?php echo $this->username; ?>
If you take a look at Zend\Mvc\Controller\Plugin\Layout you will see that the __invoke method with no parameters will return an instance of ViewModel, hence why this works.
If you want to define it module-wide, on your Module.php
public function onBootstrap(MvcEvent $e)
{
....
$e->getViewModel()->setVariable('username', 'some_value');
}
and on your layout.phtml
echo $layout->username;
In module.php u can do this
public function onBootstrap(MvcEvent $e)
{
....
$e->getViewModel()->setVariable('username', $username);
}
And in your layout u can do
echo $this->layout()->username;
Also check if u have used MvcEvent class.
Related
I have a problem, doing some product page, already got listen all products from database. And now doing view object page, to check currently product details. Doing everything by this video:
'https://www.youtube.com/watch?v=SpOaqDJ2D3A'
But it's not working for me. Writing page not found when I press to a link View product.
1. I have created: view_object.blade.php
2. Have a controller:
'public function view_brac( $BrackID )
{
return view('view_object');
}'
3. Have a route:
'Route::get('view_object&(BrackID?)','BracController#view_brac');'
4. And this is a link to View full object:
'< a href="view_object& < ?php echo $ users->BrackID? >">Plačiau< /a >'
Can some you help, why this not working?
If you want to use URI with parameter like view_object?brackId=5, change the route to:
Route::get('view_object', 'BracController#view_brac');
The controller method to:
public function view_brac()
{
return view('view_object');
}
And the link to the view:
Plačiau
Then you'll be able to get the brackId in a view or a controller method with:
{{ request('brackId') }}
First, you aren't returning anything in your controller method along with the view.
public function view_brack( $BrackID )
{
$brack = Brack::find($BrackID);
return view('view_object', compact('brack'));
}
Second, the route definition is incorrect.
Route::get('brack/{brack}','BracController#view_brack')->name('brack.show');
Last, create your anchor tag using Laravel's helper functions.
Plačiau
My site uses multiple languages and my users can click on flags to set their desired language. When that flag is clicked, a Session should store that information and then i want my controller to redirect the user to another page. This i do with the following code:
<?php
public function setLangAction () {
$oLanguageCookie = new Container('language');
$oLanguageCookie->lang = $this->params ('langvar');
$this->redirect()->toRoute('loadpage', array('page' => 'home'));
}
?>
However, when i print_r($_SESSION) in the indexAction (the action where loadpage routes to), $_SESSION is empty.
Can somebody help me?
Depending where you param comes from you should execute
$this->params()->fromQuery('langvar');
$this->params()->fromPost('langvar');
unless it is a route parameter then you can use either:
$this->params()->fromRoute('langvar');
$this->params('langvar');
I'm going through the Joomla 2.5 tutorial to build a custom component. Now I'm facing an issue on the redirection after using JToolbar::save() or JToolBarHelper::cancel for that matter. By default Joomla wants to redirect to the default layout (from the edit layout). However I don't want it to do that. I want it to redirect back to another view. In Joomla 1.5 I would have done this through adding the function into the controller - something like
function cancel()
{
//redirects user back to blog homepage with Cancellation Message
$msg = JText::_( 'COM_BLOG_POST_CANCELLED' );
$this->setRedirect( 'index.php?option=com_jjblog&view=jjblog', $msg );
}
Now that works beautifully for the cancel function, however for save this is a much more complex thing. If I want to overwrite the url do I have to redirect the controller to the model and then write in all the code for the model interaction? Because that seems slightly excessive just for a url redirection like you would in Joomla 1.5?
Hope you have added the save toolbar code with the proper controller name like this
JToolBarHelper::save('controllerName.save');
Create a save function in appropriate controller.
Add the task in the form
Finnally make sure you have added form action withthe corresponding component name.
You can try this-
In the controller firstly you call the parent save function than redirect to url.
function save(){
parent::save();
$this->setredirect('index.php?option=com_mycomponent');
}
OK it didn't need to $this->setRedirect at all. Just needed me to change the value to
protected $view_list = 'jjBlog';
which then sets the redirects of everything back to that list view.
Source link for this is here.
Thanks for all the responses though!!
view.html.php
protected function addToolbar ()
{
JRequest::setVar ('hidemainmenu', false);
JToolBarHelper::title (JText::_ ('Configuration'), 'configuration.gif');
JToolBarHelper::save($task = 'save', $alt = 'JTOOLBAR_SAVE');
}
controller.php
public function save()
{
$mainframe = JFactory::getApplication();
$mainframe->enqueueMessage (JText::_ ('COM_SOCIALLOGIN_SETTING_SAVED'));
$this->setRedirect (JRoute::_ ('index.php', false));
}
I think you can use
global $mainframe;
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
If you are overriding joomla's default save function in your custom component like
function save( $task = 'CustomSave', $alt = 'Save' ) // or even same name Save
Inside your controller you can use the CustomSave as the task and use $mainframe for redirect.
or
$mainframe = &JFactory::getApplication();
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
Hope this may help you..
After login i use need to set up redirect to custom page. How to catch this authorization in onAfterRoute event?
You should go to this path:
JOOMLAROOT/components/com_user/controller.php
in function register_save(), find this code:
if ( $useractivation == 1 ) {
$message = JText::_( 'REG_COMPLETE_ACTIVATE' );
} else {
$message = JText::_( 'REG_COMPLETE' );
}
after line put this code:
$this->setRedirect('/Your Custom Page Address', $message);
Why not just use the built in redirect in either the Joomla user login menu item or the standard Joomla login module. Both offer the option to redirect a user after a successful login. In the case of the module, you would need to create a menu item pointing to the custom page, but that's easy enough to do.
Is there something you need to do other than just a simple redirect? If not, then just use the system as it is designed.
I would create a small plugin that handles the redirect after login.
After a user has been logged in, the event onUserLogin is triggered, and you could simply do a redirect when the event is called.
Avoid any core hacks, since you'll allways end up having a hazzle during updates.
The code for a plugin like this could look like this:
class plgAuthenticationMyredirect extends JPlugin{
function onUserLogin ($user, $options){
$link = 'index.php?option=.....';
$msg = 'Message to show after login';
$app = JFactory::getApplication();
$app->redirect($link, $msg);
}
}
Ive been working with CI and I saw on the website of CI you can load a view as a variable part of the data you send to the "main" view, so, according the site (that says a lot of things, and many are not like they say ...ej pagination and others) i did something like this
$data['menu'] = $this->load->view('menu');
$this->load->view ('home',data);
the result of this is that I get an echo of the menu in the top of the site (before starts my body and all) and where should be its nothing, like if were printed before everything... I have no idea honestly of this problem, did anybody had the same problem before?
Two ways of doing this:
Load it in advance (like you're doing) and pass to the other view
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
Load a view "from within" a view:
<?php
// put this in the controller
$this->load->view('home');
// put this in /application/views/home.php
$this->view('menu');
echo 'Other home content';
Create a helper function
function loadView($view,$data = null){
$CI = get_instance();
return $CI->load->view($view,$data);
}
Load the helper in the controller, then use the function in your view to load another one.
<?php
...
echo loadView('secondView',$data); // $data array
...
?>