Passing data in Laravel between a controller and a mailable - laravel

I can't seem to get data transferred after I submit a create form of a model record to a mailable. I think I'm right on most of it, but I'm stuck at passing the data through the gap between a controller and my mailable.
I have a shipment controller with the following function:
public function store(Request $request)
{
$this->validate(request(), [
'pro_number' => 'required',
'shipment_origin' => 'required'
/*'piecesNumber' => 'required' (had to remove for now, but must review)*/
]);
$user_id = Auth::id();
$input = $request->all();
//Save Initial Shipment Data
$shipment = new Shipment();
$shipment->pro_number = request('pro_number');
$shipment->shipment_origin = request('shipment_origin');
$shipment->date = request('date');
$shipment->due_date = request('due_date');
$shipment->tractor_id = request('tractor_id');
$shipment->trailer_id = request('trailer_id');
$shipment->driver_id = request('driver_id');
$shipment->notes = request('notes');
$shipment->shipper_no = request('shipper_no');
$shipment->ship_to = request('ship_to');
$shipment->ship_from = request('ship_from');
$shipment->bill_to = request('bill_to');
$shipment->bill_type = request('bill_type');
$shipment->load_date = request('load_date');
$shipment->shipment_status = 0;
$shipment->created_by = $user_id;
$shipment->save();
//Save Shipment Details
for ($i = 0; $i < count($request->shipment_details['piecesNumber']); $i++) {
Shipment_Detail::create([
'shipment_id' => $shipment->id,
'pieces_number' => $request->shipment_details['piecesNumber'][$i],
'pieces_type' => $request->shipment_details['piecesType'][$i],
'rate_type' => $request->shipment_details['rateType'][$i],
'charge' => $request->shipment_details['charge'][$i],
'weight' => $request->shipment_details['weight'][$i],
'hazmat' => $request->shipment_details['hazmat'][$i],
'description' => $request->shipment_details['description'][$i] ]);
}
$user = Auth::user()->email;
Mail::to($user)->send(new newFreightBill($shipment));
Session::flash('success_message','Freight Bill Successfully Created'); //<--FLASH MESSAGE
//Return to Register
return redirect('/shipments/i/'.$shipment->url_string);
}
Now as you can see there is a line near the bottom "Mail::to($user)->send(new newFreightBill($shipment));", what I would like to be doing is moving the copying the data from the submitted $shipment to the mailable
My newFreightBill mailable:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Shipment;
class newFreightBill extends Mailable
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
public $shipment;
public function __construct(Shipment $shipment)
{
$shipment = $this->shipment;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('MYEMAIL')
->view('emails.shipments.created')->with(['shipment', $this->shipment]);
}
}
and my mail template (which I do receive in my tests):
<div>
Pro Number: {{ $shipment['pro_number'] }}
</div>
As you can see the template is VERY simple to say the least. I get in my emails to me the following:
Pro Number:
but absolutely nothing else, no actual number. SO I know the issue is passing the data from the form submit to the mailable variables.

If you are using properties on your Mail class such as public $shipment, you shouldn't use with() method. The properties on your class are available on your view without the need of using with.
And then you should do this: Pro Number: {{ $shipment->pro_number }}
Oh yes, and this: $shipment = $this->shipment; is backwards, it should be $this->shipment = $shipment;.

Related

How to manipulate data before exporting in laravel 5.7 and excel 3.1

I want to customize column names, concatinate data in one column. Can put some condition on data.
Below is sample image, In which format data would show. How to make possible this-
Here you can find how to manipulate columns names:
https://laravel-excel.maatwebsite.nl/3.1/exports/mapping.html
And below its a example from my project in how to use it. Enjoy!
namespace App\Exports;
use App\Aluno;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
class AlunosExport implements FromCollection, WithStrictNullComparison, WithHeadings, ShouldAutoSize
{
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
$alunos = Aluno::all();
$dadosAluno = [];
foreach ($alunos as $aluno) {
$textoEtapas = '';
foreach ($aluno->etapas as $etapa) {
$textoEtapas .= "{$etapa->nome}";
if ($etapa->concluido) {
$textoEtapas .= ' (Concluído)';
}
$textoEtapas .= "\n";
}
$dadosAluno[] = [
'nome' => $aluno->cliente->nome,
'telefone' => formatarTelefone($aluno->cliente->telefone),
'instituicao' => $aluno->cliente->turma->unidade->instituicao->nome,
'turma' => $aluno->cliente->turma->nome,
'programa' => $aluno->programa->nome,
'etapas' => $textoEtapas,
'valor' => $aluno->valor,
'orientador' => !is_null($aluno->orientador) ? $aluno->orientador->nome : '(Excluído)',
'status' => $aluno->cliente->status
];
}
$alunoColection = collect($dadosAluno);
return $alunoColection;
}
public function headings(): array
{
return [
'Aluno',
'Telefone',
'Instituição',
'Turma',
'Programa',
'Etapas',
'Valor',
'Orientador',
'Status'
];
}
}

how to fetch the children of an entity based on the user selection(i.e two entites A and B the list of B is based on the selected A )

What i need to achieve is when i select a category, then the Skills relating to that category is made available to choose from in the form. the relationship between category and skills is a manytomany rel. also i have followed this tutorial from here https://symfony.com/doc/current/form/dynamic_form_modification.html#form-events-submitted-data and have arrived at the following.
Form
class ProjectType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('category', EntityType::class, array(
'class'=>'AppBundle:Category',
'choice_label'=>'name'
))
->add('name', TextType::class, array('label' => 'Name','attr' => array('class'=>'form-control')));
$formModifier = function (FormInterface $form, Category $category = null) {
$skills = null === $category ? array() : $category->getSkill();
$skills = $this->getDoctrine()->getEntityManager()->merge($skills);
$form->add('skill', EntityType::class, array(
'class' => 'AppBundle:Skill',
'placeholder' => '',
'choices' => $skills,
));
};
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($formModifier) {
// this would be your entity, i.e. project
$data = $event->getData();
$formModifier($event->getForm(), $data->getCategory());
}
);
$builder->get('category')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($formModifier) {
// It's important here to fetch $event->getForm()->getData(), as
// $event->getData() will get you the client data (that is, the ID)
$category = $event->getForm()->getData();
// since we've added the listener to the child, we'll have to pass on
// the parent to the callback functions!
$formModifier($event->getForm()->getParent(), $category);
}
);
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) {
$event->stopPropagation();
}, 900);
Controller
$project = new Project();
$form = $this->createForm(ProjectType::class, $project)
->add('save', SubmitType::class, array(
'label' => 'Save',
'attr'=>array('class'=>'btn btn-md btn-info')
));
$form->handleRequest($request);
if ($form->isValid()) {
$project->setCreated(new \DateTime());
$project->setDiscontinue(0);
$project->setViewed(0);
$em = $this->getDoctrine()->getManager();
$em->persist($project);
$em->flush();
}
return $this->render('default/project.html.twig',array(
'form' => $form ->createView(),
));
View
{% block content%}
<div class="">
{{ form_start(form) }}
{{ form_row(form.category) }}
{{ form_row(form.skill) }}
{{ form_end(form) }}
</div>
{% endblock %}
script>
var $category = $('#project_category');
$category.change(function() {
// ... retrieve the corresponding form.
var $form = $(this).closest('form');
// Simulate form data, but only include the selected sport value.
var data = {};
data[$category.attr('name')] = $category.val();
// Submit data via AJAX to the form's action path.
$.ajax({
url : $form.attr('action'),
type: $form.attr('method'),
data : data,
success: function(html) {
// Replace current position field ...
$('#project_skill').replaceWith(
// ... with the returned one from the AJAX response.
$(html).find('#project_skill')
);
// Position field now displays the appropriate positions.
}
});
});
I Get this error
Entities passed to the choice field must be managed. Maybe persist them in the entity manager?
this is the relationship btw Category and Skill
/**
* #ORM\ManyToMany(targetEntity="Skill", inversedBy="category")
* #ORM\JoinTable(name="category_skills")
*/
private $skill;
public function __construct()
{
$this->skill = new ArrayCollection();
}
/**
* #ORM\ManyToMany(targetEntity="Category", mappedBy="skill")
*/
private $category;
public function __construct() {
$this->category = new ArrayCollection();
}
I hope this help some one out. first i give credit to #J.M. Echevarría i got this answer from here i could easily just have ask you to follow the link but that answer was 2014 so i would just update it for the sake of the newbies that might require the solution. now that that is of the way.
what i want to achieve is to let user select from a range of Categories (entity A) then have the second input Skill(entity B) based on entity A. to do that we will use 2 different select boxes: one for selecting your Category , and the next select box with all the skills for that category.
The form (projetType)
<?php
namespace AppBundle\Form\Type;
use AppBundle\Entity\Category;
//not that you have to import the entity manager to your form we will register this in our service later
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* #author Akoh Ojochuma Victor <akoh.chuma#gmail.com>
*/
class ProjectType extends AbstractType
{
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, array(
'attr' => array('class'=>'form-control','placeholder'=>'Project name e.g Paint a house' ),
));
// other filds
// Add listeners
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
$builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit'));
}
protected function addElements(FormInterface $form, Category $category = null) {
// Add the category element
$form->add('category', EntityType::class, array(
'class'=>'AppBundle:Category',
'attr'=>array("id"=>"demoSelect","class"=>"form-control"),
'choice_label'=>'name',
'required' => false,
'expanded'=>false,
'data' => $category,
'placeholder' => 'Choose a category (optional)',
));
// For the sake of this project all the skills are presented to you if you
// did not choose any particular category
$skills = $this->em->getRepository('AppBundle:Skill')->findAll();
//this would happen when the user selects a category
if ($category) {
// Fetch the skills for the selected category
$repo = $this->em->getRepository('AppBundle:Skill');
// this is your custom query for retrieving the skills from the database
$skills = $repo->findCategorySkill($category, array('name' => 'asc'));
}
//add the skills
$form->add('skill', EntityType::class, array(
'class'=>'AppBundle:Skill',
//pass in the skills gotten as choice
'choices' => $skills,
'attr'=>array("class"=>"form-control"),
'choice_label'=>'name',
'placeholder' => 'What skills are required ?',
'multiple' => true,
'required' => true,
'expanded' => false,
'label' => false,
));
}
function onPreSubmit(FormEvent $event) {
$form = $event->getForm();
$data = $event->getData();
// Note that the data is not yet hydrated into the entity.
$category =$this->em->getRepository('AppBundle:Category')->find($data['category']);
$this->addElements($form, $category);
}
function onPreSetData(FormEvent $event) {
$project = $event->getData();
$form = $event->getForm();
// We might have an empty account (when we insert a new account, for instance)
$category = $project->getSkill() ? $project->getSkill()->getCategory() : null;
$this->addElements($form, $category);
}
/**
* #param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Project',
));
}
/**
* #return string
*/
public function getName()
{
return 'app_project';
}
}
Ajax controller
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use AppBundle\Entity\Bid;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Ajax controller.
*
* #Route("/ajax")
*/
class AjaxController extends Controller
{
/**
* get skill for category with ajax when a user select a category
*
* #Route("/category/skills", name="get-category-new")
*/
public function ajaxAction(Request $request) {
if (! $request->isXmlHttpRequest()) {
throw new NotFoundHttpException();
}
// Get the province ID
$id = $request->query->get('category_id');
$result = array();
// Return a list of cities, based on the selected province
$repo = $this->getDoctrine()->getManager()->getRepository('AppBundle:Skill');
$skills = $repo->findCategorySkill($id);
foreach ($skills as $skill) {
$result[$skill->getName()] = $skill->getId();
}
return new JsonResponse($result);
}
}
Twig Template
{# ajax stuuf but if this conflict with any of your js feel free to remove or try diff version #}
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#project_category').change(function(){
var val = $(this).val();
//alert('sucess');
$.ajax({
type: "POST",
url: "{{ url('get-category-new') }}?category_id=" + val,
success: function(data) {
// Remove current options
$('#project_skill').html('');
$.each(data, function(k, v) {
$('#project_skill').append('<option value="' + v + '">' + k + '</option>');
});
}
});
return false;
});
});
</script>
repository
<?php
namespace AppBundle\Repository;
use Doctrine\ORM\EntityRepository;
/**
* SkillRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class SkillRepository extends EntityRepository
{
public function findCategorySkill($category)
{
return $this
->createQueryBuilder('l')
->select('p')
->from('AppBundle:Skill','p')
->join('p.category', 'c')
->where('c = :category')
->setParameter('category', $category)
->getQuery()
->getResult();
}
}
this is your query please note this is specific so feel free to put this to your tase.
service include this in your services
app.form_project:
class: AppBundle\Form\Type\ProjectType
arguments: ["#doctrine.orm.entity_manager"]
tags:
- { name: form.type, alias: app_project_creation }
yap thats all in case you need detailed explanation visit here here

Calling the controller i'm in : not found

I'm trying to implement the PayPal API
namespace App\Http\Controllers;
use PayPal;
use Redirect;
class PaypalPaymentController extends Controller{
private $_apiContext;
public function __construct()
{
$this->_apiContext = PayPal::ApiContext(
config('services.paypal.client_id'),
config('services.paypal.secret'));
$this->_apiContext->setConfig(array(
'mode' => 'sandbox',
'service.EndPoint' => 'https://api.sandbox.paypal.com',
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => true,
'log.FileName' => storage_path('logs/paypal.log'),
'log.LogLevel' => 'FINE'
));
}
public function getCheckout()
{
$payer = PayPal::Payer();
$payer->setPaymentMethod('paypal');
$amount = PayPal:: Amount();
$amount->setCurrency('EUR');
$amount->setTotal(500); // This is the simple way,
// you can alternatively describe everything in the order separately;
// Reference the PayPal PHP REST SDK for details.
$transaction = PayPal::Transaction();
$transaction->setAmount($amount);
$transaction->setDescription('Altaro VM Backup');
$redirectUrls = PayPal:: RedirectUrls();
$redirectUrls->setReturnUrl(action('PaypalPaymentController#getDone'));
$redirectUrls->setCancelUrl(action('PaypalPaymentController#getCancel'));
$payment = PayPal::Payment();
$payment->setIntent('sale');
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectUrls);
$payment->setTransactions(array($transaction));
$response = $payment->create($this->_apiContext);
$redirectUrl = $response->links[1]->href;
return Redirect::to( $redirectUrl );
}
public function getDone(Request $request)
{
$id = $request->get('paymentId');
$token = $request->get('token');
$payer_id = $request->get('PayerID');
$payment = PayPal::getById($id, $this->_apiContext);
$paymentExecution = PayPal::PaymentExecution();
$paymentExecution->setPayerId($payer_id);
$executePayment = $payment->execute($paymentExecution, $this->_apiContext);
// Clear the shopping cart, write to database, send notifications, etc.
// Thank the user for the purchase
return "Merci pour votre achat";
}
public function getCancel()
{
// Curse and humiliate the user for cancelling this most sacred payment (yours)
return "Erreur";
}
}
As you can see in the controller i'm trying to call other function of the controller i'm in :
$redirectUrls->setReturnUrl(action('PaypalPaymentController#getDone'));
$redirectUrls->setCancelUrl(action('PaypalPaymentController#getCancel'));
But i have the following error :
InvalidArgumentException in UrlGenerator.php line 602:
Action App\Http\Controllers\PaypalPaymentController#getDone not defined.
I don't understand how it is even possible... I checked multiple times the spelling, everything's correct here.
Any thoughts ?
EDIT:
I also tried this :
$redirectUrls->setReturnUrl(route('paypal.done'));
$redirectUrls->setCancelUrl(route('paypal.cancel'));
with these routes:
Route::get('done', [
'as' => 'paypal.done',
'uses' => 'PaypalPaymentController#getDone'
]);
Route::get('cancel', [
'as' => 'paypal.cancel',
'uses' => 'PaypalPaymentController#getCancel'
]);
It works !
I think you also have to define a route to the methods, otherwise the URL cannot be build and returns an error.

laravel validate multiple models

I would like a best practice for this kind of problem
I have items, categories and category_item table for a many to many relationship
I have 2 models with these validations rules
class Category extends Basemodel {
public static $rules = array(
'name' => 'required|min:2|max:255'
);
....
class Item extends BaseModel {
public static $rules = array(
'title' => 'required|min:5|max:255',
'content' => 'required'
);
....
class Basemodel extends Eloquent{
public static function validate($data){
return Validator::make($data, static::$rules);
}
}
I don't know how to validate these 2 sets of rules from only one form with category, title and content fields.
For the moment I just have a validation for the item but I don't know what's the best to do:
create a new set of rules in my controller -> but it seems redundant
sequentially validate Item then category -> but I don't know how to handle validations errors, do I have to merges them? and how?
a 3rd solution I'm unaware of
here is my ItemsController#store method
/**
* Store a newly created item in storage.
*
* #return Redirect
*/
public function store()
{
$validation= Item::validate(Input::all());
if($validation->passes()){
$new_recipe = new Item();
$new_recipe->title = Input::get('title');
$new_recipe->content = Input::get('content');
$new_recipe->creator_id = Auth::user()->id;
$new_recipe->save();
return Redirect::route('home')
->with('message','your item has been added');
}
else{
return Redirect::route('items.create')->withErrors($validation)->withInput();
}
}
I am very interested on some clue about this subject
thanks
One way, as you pointed yourself, is to validate it sequentially:
/**
* Store a newly created item in storage.
*
* #return Redirect
*/
public function store()
{
$itemValidation = Item::validate(Input::all());
$categoryValidation = Category::validate(Input::all());
if($itemValidation->passes() and $categoryValidation->passes()){
$new_recipe = new Item();
$new_recipe->title = Input::get('title');
$new_recipe->content = Input::get('content');
$new_recipe->creator_id = Auth::user()->id;
$new_recipe->save();
return Redirect::route('home')
->with('message','your item has been added');
}
else{
return Redirect::route('items.create')
->with('errors', array_merge_recursive(
$itemValidation->messages()->toArray(),
$categoryValidation->messages()->toArray()
)
)
->withInput();
}
}
The other way would be to create something like an Item Repository (domain) to orchestrate your items and categories (models) and use a Validation Service (that you'll need to create too) to validate your forms.
Chris Fidao book, Implementing Laravel, explains that wonderfully.
You can also use this:
$validationMessages =
array_merge_recursive(
$itemValidation->messages()->toArray(),
$categoryValidation->messages()->toArray());
return Redirect::back()->withErrors($validationMessages)->withInput();
and call it in the same way.
$validateUser = Validator::make(Input::all(), User::$rules);
$validateRole = Validator::make(Input::all(), Role::$rules);
if ($validateUser->fails() OR $validateRole->fails()) :
$validationMessages = array_merge_recursive($validateUser->messages()->toArray(), $validateRole->messages()->toArray());
return Redirect::back()->withErrors($validationMessages)->withInput();
endif;

Zend Framework 2: get matched route in view

I'm currently learning ZF2 by developing a small MVC application roughly based on the skeleton app. Right now I'm trying to hide some fixed HTML elements based on the route matched: just as an example, I don't want the main menu to show during the login phase.
I can do that easily by passing toggle parameters as return values from the controller actions, but it doesn't feel right, so I'd like to just check the matched route from the layout and compose the layout accordingly.
Problem is, I don't know how to get the matched route in the template. Is this possible? Are there other solutions to avoid adding layout logic into controllers?
Edit after some good Frankenstein work, I was able to find a solution for this. I like the idea of using a helper, so I just tried to pass it the Application object, from the boostrap function in the main module:
$app = $e->getApplication();
$serviceManager = $app->getServiceManager();
....
$serviceManager->get('viewhelpermanager')->setFactory('getRoute', function($sm) use ($app) {
return new Helper\GetRoute($app);
});
and the helper function:
use Zend\View\Helper\AbstractHelper;
class GetRoute extends AbstractHelper {
private $sm;
public function __construct($app) {
$this->sm = $app->getServiceManager();
}
public function echoRoute() {
$router = $this->sm->get('router');
$request = $this->sm->get('request');
$routeMatch = $router->match($request);
if (!is_null($routeMatch))
echo $routeMatch->getMatchedRouteName();
}
}
perhaps there's a cleaner, more ZF2ish way to do this...
Another solution without a new match
$routeMatch = $serviceLocator->get('Application')->getMvcEvent()->getRouteMatch();
echo $routeMatch->getMatchedRouteName();
There is a way to get service manager in layout:
$sm = $this->getHelperPluginManager()->getServiceLocator();
and then you can access $sm->get('router') etc.
You could create a View helper that implements ServiceManagerAwareInterface. Then inside the View helper using the ServiceManager instance to retrieve both the router and request objects then reconstruct the route match.
$services = $this->getServiceManager();
$router = $services->get('router');
$request = $services->get('request');
$routeMatch = $router->match($request);
echo $routeMatch->getMatchedRouteName();
I'd also recommend writing the View helper so that code only triggers once per request.
When moving to ZF3, you should consider use this method... since getLocator isn't available anymore (and it's not correct inject it).
Create the Helper
namespace Application\View\Helper;
use Zend\Http\Request;
use Zend\Router\RouteStackInterface;
use Zend\View\Helper\AbstractHelper;
/**
* Helper to get the RouteMatch
*/
class RouteMatch extends AbstractHelper
{
/**
* RouteStackInterface instance.
*
* #var RouteStackInterface
*/
protected $router;
/**
* #var Request
*/
protected $request;
/**
* RouteMatch constructor.
* #param RouteStackInterface $router
* #param Request $request
*/
public function __construct(RouteStackInterface $router, Request $request)
{
$this->router = $router;
$this->request = $request;
}
/**
* #return \Zend\Router\RouteMatch
*/
public function __invoke()
{
return $this->router->match($this->request);
}
}
Create a Factory for this helper
namespace Application\View\Helper;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
class RouteMatchFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$router = $container->get('router');
$request = $container->get('request');
return new RouteMatch($router, $request);
}
}
Call your Factory on your Module.php and create an Alias for it.
public function getViewHelperConfig()
{
return array(
'factories' => array(
RouteMatch::class => RouteMatchFactory::class,
),
'aliases' => array(
'routeMatch' => RouteMatch::class,
)
);
}
That's it... you have a RouteMatch Helper using the new ZF3 standards.
Bye!
In view you can use:
$this->getHelperPluginManager()->getServiceLocator()->get('request')->getUri()->getPath();
or
$this->getHelperPluginManager()->getServiceLocator()->get('request')->getUri()->toString();
I believe you can solve it by finding the action/controller names:
$controller = $this->getRequest()->getControllerName();
$action = $this->getRequest()->getActionName();
Once you know the action, you can have specific conditions to enable sections of the layout.
I view you can use
$this->getHelperPluginManager()->getServiceLocator()->get('Application')->getMvcEvent()->getRouteMatch()->getMatchedRouteName();
Additional information about "Rodrigo Boratto" post for integrating getRouteMatch in ZF3 (I can't comment because I have under 50 repo...)
In view helper file these line:
use Zend\Mvc\Router\RouteMatch as MvcRouteMatch;
use Zend\Mvc\Router\RouteStackInterface;
should be:
use Zend\Router\RouteMatch as MvcRouteMatch;
use Zend\Router\RouteStackInterface;
I don't know when they made that change but the files are in Zend\Router namespace.
P.S. I use composer if that matters.
My controller:
<?PHP
namespace SomeName\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class SomeController extends AbstractActionController
{
public function getIdAction()
{
$id = $this->params()->fromRoute('id', 0);
return new ViewModel(array(
'id' => $id,
));
}
}
My Router:
<?php
return array(
'controllers' => array(
'invokables' => array(
'SomeName\Controller\Some' => 'SomeName\Controller\SomeController',
),
),
'router' => array(
'routes' => array(
'testId' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:id]',
'constraints' => array(
'id' => '\d*',
),
'defaults' => array(
'controller' => 'SomeName\Controller\Some',
'action' => 'getId',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
At any view or layout, you are able to test route with this function:
<?php function itsRoute($routeName){
$flag = false;
if($this->serverUrl(true) == $this->url($route,[],['force_canonical'=>true]))){
$flag = true;
}
return $flag;
}

Resources