Concrete5 5.7: Passing variable from Controller to View - concrete5

So I have made a dashboard Single Page under:
/application/single_pages/dashboard/newsletter.php
and a
/application/single_pages/dashboard/view.php
The Controller is under:
/Concrete/controllers/single_pages/dashboard/newsletter.php
The Controller looks like:
<?php
namespace Concrete\Controller\SinglePage\Dashboard;
use Concrete\Core\Multilingual\Page\PageList;
use \Concrete\Core\Page\Controller\DashboardPageController;
class Newsletter extends DashboardPageController {
public function view() {
$testVar = array(
'one' => 'some',
'two' => 'value',
'three' => 'foo',
'four' => 'bar'
);
$this->set('test', $testVar);
}
}
The /application/single_pages/dashboard/newsletter.php looks like:
<?php defined('C5_EXECUTE') or die("Access Denied.");
echo 'something';
print_r($test);
The /application/single_pages/dashboard/view.php looks like:
<?php defined('C5_EXECUTE') or die("Access Denied.");
THE PROBLEM:
The local variable $test doesn't show up in the view.
The echo 'something'; is showing, so basically the page is up and running.
What am I doing wrong?

It was a path & namespace issue:
The path for the controller must be:
/application/controllers/single_page/dashboard/newsletter.php
instead of:
/concrete/controllers/single_pages/dashboard/newsletter.php
For the controller path its singular single_page but for the Single Page itself its plural single_pages
When extending from the core, we need to adapt the namespace to:
Application\Controller\SinglePage\Dashboard
instead of:
Concrete\Controller\SinglePage\Dashboard

Adding a singlepage before the controller is in place will cause it to be detached from the controller without warning, try removing it and re-adding it.

Related

Outputting A Custom Blade Partial from Sober Controller

So, for my template, I'm creating a block of code that runs custom "Modules". Basically, they are a custom post type, with fields built in ACF. Each of these modules has their own Blade template (e.g. /resources/views/modules/brandwindow.blade.php).
I've also created a Blade template for a custom post type called "Modular Page". On this template, I handle all the calls for each and any module called in a loop. I want to be able to handle this login in a controller, however. So, I created a Controller called "LoadModules" that will handle that request.
I'm having some difficulty calling a Blade template from a Controller, though. In a function GetModules(), I loop through the ACF repeater to see which module I need to load, and then supply all the relevant fields with another Controller specific to that Module (e.g. if module = 'bwindow', Call BrandWindow->BrandWindowFields). This returns an array which I can then pass into a View.
I've added the View class to the top of the Controller (like you would in Laravel), but I'm pretty sure I'm doing something wrong here. I'm getting the following error:
Class 'View' not found in ..\app\Controllers\LoadModules.php on line 25
Here's my Controller code:
public static function GetModules()
{
$brand_window_loader = new BrandWindow;
$module_array = get_sub_field('content_type');
if ($module_array == 'bwindow') {
// Do Brand Window Stuff
$windows = $brand_window_loader->GetBrandWindows();
$output = '';
foreach ( $windows as $window ){
$feild_array = $brand_window_loader->BrandWindowFields($window);
$output .= View::make('modules.brandwindow', $feild_array);
}
return $output;
} elseif ($module_array == 'blogfeed') {
// Do Blog Feed Stuff
return $module_array;
}
}
Line 25, is throwing the error which is:
$output .= View::make('modules.brandwindow', $feild_array);
The $brand_window_loader->BrandWindowFields($window) is just an array of keyed values it pulls from ACF using the ID of the post.
'title' => 'Title',
'sub_text' => 'Sub Text',
'url' => 'http://example.com'
ETC.
The error is obviously related to the "View" class not existing, so I'm wondering if I've namespaced or the View class does not exist with Sage 9/Bedrock. If that's true, what is the best way to include a Blade template from a Controller?
Thanks
You can try :
$output .= App\template('modules.brandwindow', $feild_array)

How can I pass variables to login and register view in laravel 5.3

I am learning and developing a project in laravel 5.3. So I stucked at a point that, to every view in this project I am passing variables to views like following.
public function index()
{
$page_title = 'Page Title';
return view('home', ['title' => $page_title]);
}
so in login and register controllers there are no methods to return views. And I want to pass same variables with different string values to login and registration form. so how can i do that. And secong thing I want to ask is, that how can I add a 404 error page in my project for undefined routes. and third question is that can I set 404 page to register route (www.myproject.com/register) after adding some users in my project. looking forword for reply..
The methods to return the views are in traits, if you want to add you own logic for these methods you can simply override them by adding your own methods the class that uses the trait e.g.
RegisterController
public function showRegistrationForm()
{
$title = 'Register';
return view('auth.register', compact('register'));
}
LoginController
public function showLoginForm()
{
$title = 'Login';
return view('auth.login', compact('title'));
}
If you want to add a custom 404 error page then you just need to create that a file at resources/views/errors/404.blade.php as shown in the docs https://laravel.com/docs/5.4/errors#custom-http-error-pages
Laravel comes with a RedirectIfAuthenticated middleware that will (as the name suggests) rediect the user away from a route if they are already logged in. By default, the login and register routes already have this. If you want to change this behaviour just edit your App\Http\Middleware\RedirectIfAuthenticated class.
Hope this helps!
In the login controller > AuthenticatesUsers trait you can type your variables here.
Default path: app/Http/Controllers/Auth/LoginController.php
public function showLoginForm()
{
$test = 'test';
return view('auth.login', compact('test'));
}
Optimized this could save your 'arss'
LoginController.php
public function showLoginForm()
{
$title = 'Login';
$css = array(
asset('sign-up-in/css/index.css'),
);
$js = array(
asset('sign-up-in/js/index.js'),
);
return view('auth.login', compact('title','css','js'));
}
Do the same for RegisterController.php +(bonus) every other controller
Now slay it all with this
app.blade.php
#if(isset($js))
#foreach($js as $key => $value)
<script src="{{$value}}"></script>
#endforeach
#endif
#if(isset($css))
#foreach($css as $key => $value)
<link href="{{$value}}" rel="stylesheet">
#endforeach
#endif

How to set form action in Symfony2? (2.8 LTS)

So I've been playing around with Symfony forms and I want to change the form action.
I've followed this guide but I don't understand what it means by "target_route". As such, I was getting an error message (see below)
I have the code below and I'm pretty sure the route I used in setAction is valid since I can browse it using my browser.
Any ideas? Thank you
my code:
<?php
// src/AppBundle/Controller/DirectoryController.php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
class DirectoryController extends Controller {
/**
* #Route("/directory/form")
*/
public function formAction() {
$form = $this->get("form.factory")
->createNamedBuilder("form", "form")
->setAction($this->generateUrl("/directory/search"))
->setMethod("get")
->add("search", "submit", array("label" => "Search"))
->add("reset", "reset", array("label" => "Reset"))
->getForm();
return $this->render(
"directory/form.html.twig",
array("form" => $form->createView()
,
)
);
}
/**
* #Route("/directory/search")
*/
public function searchAction() {
return $this->render(
"directory/view.html.twig"
);
}
}
error message:
Unable to generate a URL for the named route "/directory/search" as such route does not exist.
In the example, target_route is the name of a route, not its url. For example, you might define an action like this:
/**
* #Route("/directory/search", name="directory_search")
*/
public function searchAction() {
In that case, your route would have a name of directory_search. You would then use $this->generateUrl('directory_search') to have the router turn the name into a url.
The reason you do it this way (as opposed to using urls directly) is that this allows you to change a url without having to change every place in your code that references it.
->setAction($this->generateUrl("/directory/search"))
setAction() expects a url. So you while you can give it '/directory/search', best practice would be to it $this->generateUrl('directory_search').

Symfony2 KnpMenuBundle - Following tutorial and came across this error

I followed this tutorial:
https://github.com/KnpLabs/KnpMenuBundle/blob/master/Resources/doc/index.md#installation
And have come across the following error:
An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "page_show" as such route does not exist.") in /var/www/bundles/src/Acme/DemoBundle/Resources/views/Default/index.html.twig at line 4.
Is there a step I am missing here to pass something to a controller?
From link:
use Knp\Menu\FactoryInterface;
use Symfony\Component\DependencyInjection\ContainerAware;
class Builder extends ContainerAware
{
public function mainMenu(FactoryInterface $factory, array $options)
{
$menu = $factory->createItem('root');
$menu->addChild('Home', array('route' => 'homepage'));
$menu->addChild('About Me', array(
'route' => 'page_show',
'routeParameters' => array('id' => 42)
));
// ... add more children
return $menu;
}
}
To actually render the menu, just do the following from anywhere in any Twig template:
{{ knp_menu_render('AcmeDemoBundle:Builder:mainMenu') }}
Do a ./app/console router:debug - it will show you all the routes registered in your application. I am guessing page_show is not one of them.
The documentation you are using probably expects you to add your own routes/pages to the menu like this:
$menu->addChild('Home', array('route' => 'homepage'));
Where 'homepage' has to already exist. So does 'show_page'. So you need a controller somewhere that handles a request to the show_page route, or exchange show_page for a route that you have already defined in your app. Hope I made sense.
Following the tutorial exactly, this error is caused by line 25 in the file
2 // src/Acme/MainBundle/Menu/MenuBuilder.php
...
25 $menu->addChild('Home', array('route' => 'homepage'));
The tutorial code assumes that you have a route called 'homepage'. Assuming you set this up inside a custom Bundle, then a quick way to solve this problem so you can get the tutorial up and running is to go to...
// src/Acme/MainBundle/Resources/config/routing.yml
...and copy the homepage route from there (will look something like acme_main_bundle_homepage)

Unit tests failing when I use a custom view helper in the layout

So I have created my custom view helper and used it in layout.phtml like this:
<?php echo $this->applicationBar(); ?>
It is working flawlessly in the browser but my unit tests that were working before are failing now:
1) UnitTests\Application\Controller\IndexControllerTest::testIndexActionCanBeAccessed
Zend\ServiceManager\Exception\ServiceNotFoundException: Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for applicationBar
When I comment out the view helper in the layout file, test passes again.
I have the same problem, and i solved it in not a good way (but it solved for my specific problem).
The phpunit tests is not finding my factories view helpers, but it is finding my invokables. Then, i did the following:
public function getViewHelperConfig() {
return array(
'factories' => array(
'aplicationBar' => function($service) {
$applicationBar = new ApplicationBar();
return $applicationBar;
},
),
'invokables' => array(
'applicationBar' => 'Application\View\Helper\ApplicationBar',
),
);
When i use the browser, it uses the correct Factory. When i use phpunit, it uses the invokables.
The problem happens when i need to set some parameters. And then, i set some default parameters, that will be used only by phpunit.
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
class ApplicationBar extends AbstractHelper {
protected $parameter;
public function __construct($parameter = 'something') {
$this->parameter = $parameter;
}
public function __invoke() {
return $this->parameter;
}
}
It is not the best solution, but if i solve it in a better way, i will post here.

Resources