Why can't Kohana find my controller? - kohana

I have the following controller:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Static extends Controller_DefaultTemplate {
public function action_index()
{
View::set_global('message', '<span class="highlight">This is a global message.</span>');
$data = array (
'siteTitle' => 'Kohana Test Site',
'siteSubtitle' => 'A site to learn Kohana',
'menu' => View::factory('blocks/menu'),
);
$view = View::factory('templates/layout', $data);
$this->request->response = $view->render();
}
}
but kohana gives me the error:
ErrorException [ Fatal Error ]: Class
'Controller_DefaultTemplate' not found
although Eclipse can find the file (via F3) and I thought Kohana was able to find all classes via autoloading?
How can I get Kohana to find the Controller_DefaultTemplate class so I can extend Controller_Static?

You must include file with definition of Controller_DefaultTemplate

The problem was that my file name defaultTemplate.php was camel case, changing it to all-lowercase defaultemplate.php enabled Kohana to find the class inside it.

Related

Find a div in a block render in Symfony

I am doing a site with symfony 4. I start on this framework and I have a problem of design on my site.
I've been looking for where it might come from, but I can not find the code I'd like to remove.
The part of the code I would like to remove is the following because the result is pretty ugly:
I can not find this in my code:
in my controller :
/**
* #return Response
*/
public function newLetterAction(Request $request): Response
{
$form = $this->createForm(CustomerNewsletterType::class, new Customer());
$form->handleRequest($request);
$facebook = $this->manager->getRepository(ExternalUrl::class)->findOneByCode('facebook');
$instagram = $this->manager->getRepository(ExternalUrl::class)->findOneByCode('instagram');
return $this->templatingEngine->renderResponse('#SyliusShop/Homepage/_newsletter.html.twig', [
'facebook' => $facebook,
'instagram' => $instagram,
'form' => $form->createView(),
'rova_refonte' => (in_array($this->container->get('request_stack')->getMasterRequest()->attributes->get('_route'),["sylius_shop_homepage"]) ? true : false)
]);
}
in my formType :
class CustomerNewsletterType extends AbstractResourceType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', EmailType::class, [
'label' => 'app.ui.newsletter',
'attr' => [
'placeholder' => 'app.ui.email'
]
])
;
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix(): string
{
return 'app_customer_newsletter';
}
}
in my twig:
{{ render(controller('app.controller.shop_homepage:newLetterAction')) }}
if anyone could tell me how to find the code, it would help me a lot.
Thank you
Everything is done under the hood when you call $form->createView().
To sum up, every type of field into the form has a base rendering using twig blocks (so does the form itself), which can be overriden. This is what is called the form theme, there's a base one which is usually this one in the twig-bridge.
You can create new themes, extend the existing one, or event create just what you need for a specific form (hint : the getBlockPrefix function in your form type is used for this).
You can find all the documentation about the form rendering here :
https://symfony.com/doc/current/form/form_customization.html
Most functions described in this documentation are in fact calling twig blocks of form themes, and you can find the documentation about this here :
https://symfony.com/doc/current/form/form_themes.html
Keep in mind: Removing such a class / div might break existing CSS, error rendering or everything done in javascript targeting this class.
Most likely you use a bootstrap 3/4 form theme and there works standard form_row template layout.
To customize current form/another form elements, use "How to Work with Form Themes" tutorial.

Dompdf in services not found; Controller error "Message: Class 'Dompdf' not found"

I am working with symphony and slim, so I use services that can be called in my controllers, then to my twig views. I installed Dompdf with composer and added it to my list of services I my bootstrap folder. I have tried several ways to call dompdf, but still get
Message: Class 'Dompdf' not found
This is the code in my controller:
class SlipController extends \App\Controllers\Base\PageController{
function getHandler($request, $response, $args)
{
// Instantiate Dompdf with our options
$dompdf = new Dompdf();
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser (force download)
$dompdf->stream("mypdf.pdf", [
"Attachment" => true
]);
return $this->view->render($response,'pages/slips.twig');
}
In the services.php
<?php
$container['dompdf'] = function($container) {
return new \Dompdf\Dompdf;
};
Did you add a use statement to your code?
use Dompdf\Dompdf;
class SlipController extends \App\Controllers\Base\PageController{
function getHandler($request, $response, $args)
{
...

Slim not recogninsing classes from generated-classes file when combined with propel

When I run Propel through Slim, the Propel's generated classes are not getting loaded, and i am unable to post or get data from Database.
I have integrated Propel with Slim. All my db code is in Propel files.
In Foo.php
<?php
namespace App;
//propels required files
class Foo extends someclass{
public function foodata()
{
echo "hellow propel-Slim";
$info = new Info(); // this class is in generated_classes
$info->setInfodetails('medical info');
}
}
?>
In index.php
<?php
//Slim's required files
$app->post("/details/information", "\App\Foo:foodata");
?>
So whenever I run /details/information url it should call the function foodata and insert the data in table info using class Info.
Instead it will just print the echo statement and give me error.
hellow propel-Slim
Fatal error: Class 'App\Info' not found in foo.php.

Kohana - one function on every controller

I have a project in Kohana 3.3.
I have a lot of controllers, models etc.
Now, I want add one functionality - close the whole site for all users.
Where I can add function, which for example, will redirect users to http://mypage.com/website_is_close ?
Example:
function check(){
$isClose = DB::query(.....)
if($isClose) header("Location: http://mypage.com/website_is_close");
return false;
}
Thanks :)
In Controller_Base all other controllers extend from. E.g.
File application/classes/Controller/Base.php
class Controller_Base extends Controller_Template {
public function before()
{
$isClose = DB::query(.....)
if($isClose)
{
HTTP::redirect("http://mypage.com/website_is_close");
exit ;
}
parent::before();
}
}
All other classes should extend from that class e.g
class Controller_Home extends Controller_Base {}
I personally use this also for every subdirectory e.g.
// As all controllers in the user folder probably need to be supplied with a user anyway
class Controller_User_Base extends Controller_Base {}
class Controller_User_Profile extends Controller_User_Base {}
I think a better approach would be to add a "catch all" route to the beginning of your routes list.
It would catch all URLs and would point to a controller that you would create. This is far cleaner than hacking away at a base controller.
How does this look?
Route::set('closed', '(<url>)', array('url' => '.*'))
->defaults(array(
'controller' => 'Closed',
'action' => 'index',
));

Kohana ErrorException [ Fatal Error ]: Call to undefined method Request::redirect()

I'm using Kohana 3.3.0 and i have a controller which is supposed to save blog articles to a database then redirect to the homepage, my code is as follows:-
class Controller_Article extends Controller {
const INDEX_PAGE = 'index.php/article';
public function action_post() {
$article_id = $this->request->param('id');
$article = new Model_Article($article_id);
$article->values($_POST); // populate $article object from $_POST array
$article->save(); // saves article to database
$this->request->redirect(self::INDEX_PAGE);
}
The article saves to database but the redirect line gives the error:-
ErrorException [ Fatal Error ]: Call to undefined method Request::redirect()
Please let me know how i can do the redirect.
Thanks
You're getting the Exception because as of Kohana 3.3, Request no longer has the method redirect.
You can fix your example by replacing
$this->request->redirect(self::INDEX_PAGE);
with
HTTP::redirect(self::INDEX_PAGE);
Yeah, Request::redirect is not longer exists. So in order to easily to move from 3.2 to 3.3 I extented Kohana_Request class and added redirect method. Just create Request.php in classes folder and write
class Request extends Kohana_Request {
/**
* Kohana Redirect Method
* #param string $url
*/
public function redirect($url) {
HTTP::redirect($url);
}
}
So you will be able to use both Request::redirect and $this->request->redirect
in your controller $this->redirect('page');
$this->redirect('article/index');

Resources