Setting up magic routes for plugins in CakePHP 1.3? - cakephp

I'm working on upgrading my project from CakePHP 1.2 to 1.3. In the process, it seems that the "magic" routing for plugins by which a controller name (e.g.: "ForumsController") matching the plugin name (e.g.: "forums") no longer automatically routes to the root of the plugin URL (e.g.: "www.example.com/forums" pointing to plugin "forums", controller "forums", action "index").
The error message given is as follows:
Error: ForumsController could not be found.
Error: Create the class ForumsController below in file: app/controllers/forums_controller.php
<?php
class ForumsController extends AppController {
var $name = 'Forums';
}
?>
In fact, even if I navigate to "www.example.com/forums/forums" or "www.example.com/forums/forums/index", I get the same exact error.
Do I need to explicitly set up routes to every single plugin I use? This seems to destroy a lot of the magic I like about CakePHP. I've only found that doing the following works:
Router::connect('/forums/:action/*', array('plugin' => 'forums', 'controller' => 'forums'));
Router::connect('/forums', array('plugin' => 'forums', 'controller' => 'forums', 'action' => 'index'));
Setting up 2 routes for every single plugin seems like overkill, does it not? Is there a better solution that will cover all my plugins, or at least reduce the number of routes I need to set up for each plugin?

I guess, that topic Configuration-and-application-bootstrapping covers that:
App::build(array(
'plugins' => array('/full/path/to/plugins/', '/next/full/path/to/plugins/')
));
Also take a look at this ticket: http://cakephp.lighthouseapp.com/projects/42648/tickets/750-plugin-route-problem-when-acl-and-auth-components-used#ticket-750-5 (Cake 1.3 had removed magic plugin routes).

You don't have myplugin_app_controller.php in your /app/plugins/myplugin directory.
Just create a file containing following:
<?php
class MypluginAppController extends AppController {
}
?>
And you will have all your plugin's features. :)

Related

CakePHP 2 : Implement CakePDF plugin by Ceeram

I'm trying to implement the CakePDF plugin designed by Ceeram.
I'm using CakePHP 2 and I work locally using wamp on windows vista. I followed everything from the readme file but I got stucked at some point.
What I'd firstly like to do is converting an HTML link to a PDF using the WkHtmlToPdf engine. I see many people having issues to make it work so I'm gonna detail all the way through in the following different steps.
STEP 1: CakePdf plugin by Ceeram
I downloaded the plugin at https://github.com/ceeram/CakePdf
I extracted the contained folder into app/Plugin/CakePdf
STEP 2: Bootstrap
I added the following lines - app/Config/bootstrap.php :
CakePlugin::load('CakePdf', array('bootstrap' => true, 'routes' => true));
Configure::write('CakePdf', array(
'engine' => 'CakePdf.WkHtmlToPdf'
),
'orientation' => 'portrait',
'download' => true
));
STEP 3: Controller
I've created my controller "InvoicesController.php" - app/Controller/InvoicesController.php:
class InvoicesController extends AppController {
public $components = array('RequestHandler');
public function view($id = null) {
$this->Invoice->id = $id;
if (!$this->Invoice->exists()) {
throw new NotFoundException(__('Invalid invoice'));
}
$this->pdfConfig = array(
'orientation' => 'portrait',
'filename' => 'Invoice_' . $id
);
$this->set('invoice', $this->Invoice->read(null, $id));
}
}
STEP 4: View
I've created a pdf folder in my view folder and created view.ctp in app/View/Invoices/pdf/view.ctp.
STEP 5: Layout
I've created a pdf folder in my layout folder and created app/View/Layouts/pdf/default.ctp
That's about it. In my view I couldn't make a PDF file from an URL.
Although I have to mention I'm new in OOP and CakePHP so I would be very grateful if you could show me how to have this done. I'm sure it will help others because there many newbies like me who want to do this but because it's all meant for advanced programmers we cannot figure out how to put the pieces together.
Thank you very much for your understanding and your help!
[THIS POST IS MODIFIED EACH TIME THERE IS A NEW ANSWER WHICH IMPROVES IT]
You need to add RequestHandler component, and browse to localhost/invoices/view/1.pdf
Looks like i had forgotten to mention to add RequestHandler component in the readme.
Also for WkHtmlToPdf you need to tell it where it can find it, and since you are on windows the default location surely wont work for you. You can set the location with Configure::write('CakePdf.binary', 'C:\Program Files\wkhtmltopdf\wkhtmltopdf.exe') after having installed it on windows
You are missing this code in app/config/routers.php
Router::parseExtensions();
Router::setExtensions(array('json', 'xml', 'rss', 'pdf'));
details available on:
http://www.dereuromark.de/2014/04/08/generating-pdfs-with-cakephp/

AuthComponent not available in certain controller?

I have written my cake app to log in registered users and it works great on view pages where there is a db model associated with a User. However, on my main pages that aren't necessarily accessing some model (the PagesController where pages are things like upcoming events, contact us, about, etc), AuthComponent is not available, or at the least, the array returns empty---so i cannot retrieve, say, the username of the person logged in.
I tried creating a model called Page that belongsTo User but that didn't fix my problem.
To explain a little further, my app shows lists of certain lodgings, nightclubs, restaurants and things to do for a given city, all of which is shown whether a user is logged in or not. I don't understand where I am going wrong and why.
Here is my AppController:
<?php
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'view'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'index')
)
);
function beforeFilter() {
$this->Auth->allow('login', 'logout','index', 'view', 'condos', 'houses', 'hotels_and_motels', 'print_all_coupons', 'print_coupon', 'search', 'golf', 'charters', 'events', 'nightlife', 'shopping', 'visitors_info', 'contact_us');
}
}
?>
here is where I access my username in my default page layout:
<?php if(AuthComponent::user('id')) {
echo '<span style="font-variant:small-caps">Hello, '.AuthComponent::user('username').'</span> | ';
?>
I have found the answer, after weeks of beating my head on the wall about this. And oh, you're gonna laugh. For any of you out there up against this problem, the answer is.....make sure there is no white space before or after your tags. That is all. Seriously. Since PHP prints out everything that isn't in a PHP tag, if it finds something to send, it will send out headers before everything else. Once headers are sent, a PHP session cannot be created. So chalk this one up to the head-smacking file!
Without seeing any code my first instinct is to say that you are not loading the component in AppController but in the UsersController only. Are you loading the Auth component in AppController?
UPDATE:
Here is how I would suggest accessing the User information.
$this->Session->read('Auth.User.username');
UPDATE 2
Remove the $components declaration from the Pages Controller. It could be overriding the session.
UPDATE 3
If you have a custom Pages controller, it is probably conflicting with the Core Pages controller. Remove your Pages controller and see if it works correctly. The Core Pages controller will still route to the Views/Pages/ directory for displaying content.

cakePHP router, route isn't working

I have this route:
Router::connect(
'/:controller/*',
array('controller'=>'con3'),
array('controller'=>'con1|con2')
);
I am trying to direct every call to
/con1/x1/x2
to
/con3/x1/x2
and
/con2/y1/y2
to
/con3/y1/y2
it is not working, why ?
If you require to route /con3/ to /con1/ and/or /con2/ based on your own constraints what you require is a Custom Route class. For this there is no better place than Mark Story's tutorial on custom Route classes.
Otherwise, you could of course just extend your controllers (and leave the body empty) like this:
<?php
class Con3Controller extends Con1Controller{
// maybe add model here if you don't have
// var $uses in Con1Controller
// otherwise, extend is just fine
}
?>
In this case you don't need to mess with connecting routes like you are right now. Object inheritance will take care of your "aliasing" for you.
Have you considered something like:
Router::connect( '/con1/:action/*', array( 'controller' => 'con3' ) );
Router::connect( '/con2/:action/*', array( 'controller' => 'con3' ) );

CakePHP 1.1 "parseExtension" inexsistent

I have a problem with a site in CakePHP 1.1 which is impossible to migrate from version due to the size of the project.
I need to create methods to which call using extensions, for example:
$Route->connect('/xxx.xml', array ('controller' => 'Interactive', 'action' => 'xxx'));
But this does not work and the problem is that Cake 1.1 does not have this function:
Router::parseExtensions('xml');
Has anyone working with cakephp 1.1 had this problem? If so do they know how to fix it?
Thanks.
I'm not 100% familiar of the capabilities of CakePHP 1.1, but have you considered setting up a router to look for something like the following:
/:controller/:action.xml
Then you could control what controller/action it leads to and alter the layout.
You might need to escape the . on .xml.
I would seriously consider upgrading to CakePHP 1.3 and reading the migration docs.
The problem is not the layout, is this:
if i set this router:
$Route->connect('/xxx.xml', array ('controller' => 'Interactive', 'action' => 'xxx'));
or your example:
$Route->connect('/:controller/:xxx.xml', array ('controller' => 'Interactive', 'action' => 'xxx'));
the function called is:
class MuControlController extends AppController {
function xxx.xml() {
}
obiusly this function cant exists.

HtmlHelper not loaded in tutorial example

I followed the tutorial to create a blog for CakePHP 1.3 up to this step but keep getting error when running the app:
Notice (8): Undefined property: View::$Html [APP\views\posts\index.ctp, line 27]
Line 27:
echo $this->Html->link($post['Post']['title'], array('controller' => 'posts', 'action' => 'view', $post['Post']['id']));
Apparently CakePHP doesn't load the HtmlHelper class, I check over and over again in my controller, the Html should be loaded properly.
class PostsController extends AppController {
var $name = 'Posts';
var $helpers = array('Html', 'Form');
function index() {
$this->set('posts', $this->Post->find('all'));
}
}
When I added this line to the view (index.ctp), it works
$this->Html = &$this->loaded['html'];
But apparently I can't do that for every ctp file. I'm running Windows 7, WAMP 2, PHP 5.3.5, CakePHP 1.3.7 stable.
Anyone has a clue?
Like the comment by mtnorthrop above:
Is the FormHelper being loaded in your
views? What do you get if you do
pr($this->Html) in your view? How
about pr($html)? Until CakePHP 1.3,
helpers were accessed directly instead
of through the View object. In CakePHP
1.3 both methods should work. Does the plain $html->link() or $form->input()
methods work for you? – mtnorthrop 51
mins ago
From the book:
"The HtmlHelper is available in all
views by default. If you're getting an
error informing you that it isn't
there, it's usually due to its name
being missing from a manually
configured $helpers controller
variable."
You shouldn't need to specify it in your controllers. Perhaps somehow doing so is interfering with the core? BTW, you don't need to specify Form either.

Resources