[HowTo] Convert 3.0 MOD into 3.1 Extension

Discussion forum for Extension Writers regarding Extension Development.
neufke
Registered User
Posts: 46
Joined: Thu Jun 05, 2014 7:12 pm

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by neufke »

@nick: maybe important to mention that all

Code: Select all

$this->helper->url('someclass');
 has been replaced by 

Code: Select all

$this->helper->route('routingname', array('name' => 'class_function'); 
Therefore all pre-3.1.0b4 examples aren't working anymore... ;-)
User avatar
nickvergessen
Former Team Member
Posts: 4397
Joined: Mon Apr 30, 2007 5:33 pm
Location: Stuttgart, Germany
Name: Joas Schilling

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by nickvergessen »

neufke wrote:@nick: maybe important to mention that all

Code: Select all

$this->helper->url('someclass'); 
 has been replaced by 

Code: Select all

$this->helper->route('routingname', array('name' => 'class_function');
Therefore all pre-3.1.0b4 examples aren't working anymore... ;-)
Thanks for pointing this out.
I fixed it:
https://github.com/nickvergessen/howto- ... 56a70558ac
No Support via PM
User avatar
nickvergessen
Former Team Member
Posts: 4397
Joined: Mon Apr 30, 2007 5:33 pm
Location: Stuttgart, Germany
Name: Joas Schilling

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by nickvergessen »

Updated the composer.json file with teh changes from viewtopic.php?f=461&t=2250471
No Support via PM
User avatar
RiccardoB.
Registered User
Posts: 38
Joined: Sat Nov 27, 2010 9:54 am
Location: Italy
Name: Riccardo

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by RiccardoB. »

The rules described in this topic are all still valid for latest 3.1.0-RC3 version?
Freelance Software Developer for Web, Desktop and Mobile.
User avatar
RiccardoB.
Registered User
Posts: 38
Joined: Sat Nov 27, 2010 9:54 am
Location: Italy
Name: Riccardo

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by RiccardoB. »

I have a couple of questions about the controller: years ago I've developed a MOD and my main file starts like this:

Code: Select all

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/message_parser.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/feedback');
$user->add_lang('posting');

page_header($user->lang['FB_TITLE']);

$template->assign_block_vars('navlinks',array(
	'FORUM_NAME'	=> $user->lang['FB_FEEDBACK'],
	'U_VIEW_FORUM'	=> append_sid("{$phpbb_root_path}feedback.$phpEx"),
));

if(!$auth->acl_get('u_fb_access'))
{
	trigger_error($user->lang['FB_CANNOTACCESS']);
}

$mode = request_var('mode', '');
switch($mode)
{
	case 'feedback':
		show_feedback(0);
		break;
	case 'myfeedback':
		show_feedback(1);
		break;
	case 'add':
		add_feedback();
		break;
	case 'edit':
		edit_feedback();
		break;
	case 'delete':
		delete_feedback();
		break;
	case 'best';
		best_feedback();
		break;
	case 'worst';
		worst_feedback();
		break;
	default:
		index();
}

$template->set_filenames(array(
	'body' => 'feedback_body.html'
));

page_footer();
exit();
Now I have to encapsulate all in the controller class, but what I have to mantain and what it has to be removed?

These lines are to be kept in the controller or now are managed by phpBB?

Code: Select all

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/feedback');
$user->add_lang('posting');
These includes are still valid or the method to include them is changed?

Code: Select all

include($phpbb_root_path . 'common.' . $phpEx);
include_once($phpbb_root_path . 'includes/message_parser.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
And for the switch that select what to output (based on a $_GET var) I can create in the routing.yml, just one route or I have to create one route for each mode?

Code: Select all

$mode = request_var('mode', '');
switch($mode)
{
   case 'feedback':
      show_feedback(0);
      break;
   case 'myfeedback':
      show_feedback(1);
      break;
[ ... ]
   default:
      index();
}

$template->set_filenames(array(
   'body' => 'feedback_body.html'
));

page_footer();
exit();
Sorry for asking so many questions :?
Freelance Software Developer for Web, Desktop and Mobile.
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 6050
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by MattF »

Although this is not neccessarily exactly what it needs to be to work, a quick conversion of your old controller to the new look would be something like this (just to help you see what it could end up looking like):

Code: Select all

<?php

namespace vendor\feedback\controller;

class controller
{
    protected $auth;
    protected $helper;
    protected $template;
    protected $user;

    public function __construct(\phpbb\auth\auth $auth, \phpbb\controller\helper $helper, \phpbb\template\template $template, \phpbb\user $user)
    {
        $this->auth = $auth;
        $this->helper = $helper;
        $this->template = $template;
        $this->user = $user;
    }

    public function display($mode)
    {
        $this->user->add_lang_ext('vendor/feedback', 'feedback');

        if (!$this->auth->acl_get('u_fb_access'))
        {
           trigger_error($this->user->lang('FB_CANNOTACCESS'));
        }

        switch($mode)
        {
           case 'feedback':
              show_feedback(0);
              break;
           case 'myfeedback':
              show_feedback(1);
              break;
           case 'add':
              add_feedback();
              break;
           case 'edit':
              edit_feedback();
              break;
           case 'delete':
              delete_feedback();
              break;
           case 'best';
              best_feedback();
              break;
           case 'worst';
              worst_feedback();
              break;
           default:
              index();
        }

        $this->template->assign_block_vars('navlinks', array(
            'U_VIEW_FORUM'        => $this->helper->route('vendor_feedback_controller'),
            'FORUM_NAME'        => $this->user->lang('FB_FEEDBACK'),
        ));

        return $this->helper->render('feedback_body.html', $this->user->lang('FB_TITLE'));
    }
}
View the Acme Demo Extension for more guide on a developing extension
https://github.com/nickvergessen/phpbb-ext-acme-demo
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
RiccardoB.
Registered User
Posts: 38
Joined: Sat Nov 27, 2010 9:54 am
Location: Italy
Name: Riccardo

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by RiccardoB. »

VSE wrote:Although this is not neccessarily exactly what it needs to be to work, a quick conversion of your old controller to the new look would be something like this [...]
Thanks a lot for your code, it will be of GREAT help :)

Another question: I see that in your new class entry point, these includes are no more present:

Code: Select all

include_once($phpbb_root_path . 'includes/message_parser.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_privmsgs.' . $phpEx);
so I suppose that to parse messages and to manage PMs the method is changed respect to 3.0.x version.

Am I right? I have to include specific classes and use their methods? Where I can find these infos?

My old code to parse a message and send a PM was like this:

Code: Select all

$pm_parser = new parse_message();
$pm_parser->message = sprintf($lang['FB_NEWFEEDBACKMSG'], $vote_text, $user->data['username']);
$pm_parser->parse(true, true, true, false, false, true, true);
$pm_data = array(
	'from_user_id'		=> $user->data['user_id'],
	'from_user_ip'		=> $user->ip,
	'from_username'		=> $user->data['username'],
	'enable_sig'		=> false,
	'enable_bbcode'		=> false,
	'enable_smilies'	=> false,
	'enable_urls'		=> false,
	'icon_id'			=> 0,
	'bbcode_bitfield'	=> $pm_parser->bbcode_bitfield,
	'bbcode_uid'		=> $pm_parser->bbcode_uid,
	'message'			=> $pm_parser->message,
	'address_list'		=> array('u' => array($to_id => 'to')),
);
submit_pm('post', $lang['FB_NEWFEEDBACK'], $pm_data, false);
Freelance Software Developer for Web, Desktop and Mobile.
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 6050
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by MattF »

Those files are not namespaced so they will still need to be included where you need them.
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
vipaka
Registered User
Posts: 493
Joined: Sun Aug 28, 2011 7:25 pm

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by vipaka »

I'm a little confused after trying to edit some basic SQL query into the hello world controller page (thread above this). It looks like the $db variable is no longer recognized(Fatal error: Call to a member function sql_query()), but it shows in many of the root files so I'm guessing there is a routing issue or is it another renamed variable?

For simplicity, how exactly would you translate the following query from a 3.0 mod format into a 3.1 query in the extension's controller main file?

Code: Select all

$sql = "SELECT column
	FROM tablename
	ORDER BY column";
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result))
	{
		//do stuff	
	}
This is an excellent thread to put up with the Ascraeus release, and it has helped me understand the new file structure expected with extensions immensely.
Curious about my work? See it for yourself.
Image
nicofuma
3.2 Release Manager
3.2 Release Manager
Posts: 546
Joined: Sun Apr 13, 2014 1:47 am
Location: Grenoble - France

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by nicofuma »

Use the dependency injection to get the dbal object (it's the service dbal.conn IIRC), in your controller affect it to a field ($db per example) and then use it ($this->db).

EDIT: If you don't know how to use the dependency injection you can find a lot of examples in the core or in the extensions in development, look at the .yml (but not routing.yml) files inside the config/ directory of the core or of the extensions)
Member of phpBB Development-Team
No Support via PM
sopi
Registered User
Posts: 91
Joined: Mon Nov 24, 2008 11:35 am

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by sopi »

I'm at a loss.
First I tried to do "my own thing" but I always got an error:

Code: Select all

No route found for "GET /home"
where "home" was the subfolder containing "my" extension.

So I thought it had to be me and downloaded this extension from github. Placing all the files and folders in a folder:
ext/nickvergessen/newspage

Again I tried to access the page via:
http://localhost/phpBB3/app.php/newspage

And got the same errormessage as before:

Code: Select all

No route found for "GET /newspage"
What am I doing wrong? It has to be something very stupid :shock: :? :oops:

Cheers
sopi :roll:
User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 18489
Joined: Thu Jan 06, 2005 1:30 pm
Location: Fishkill, NY
Name: David Colón

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by DavidIQ »

Why are you trying to access that from app.php? Should just be phpBB3/newspage
Apply to become a Jr. Extension Validator
My extensions | In need of phpBB services? | Was I helpful today?
No unsolicited PMs unless you're planning on asking for paid help.
sopi
Registered User
Posts: 91
Joined: Mon Nov 24, 2008 11:35 am

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by sopi »

head >> desk
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 6050
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by MattF »

sopi wrote:Again I tried to access the page via:
http://localhost/phpBB3/app.php/newspage

And got the same errormessage as before:

Code: Select all

No route found for "GET /newspage"
What am I doing wrong? It has to be something very stupid :shock: :? :oops:
Not visiting the right url. It's http://localhost/phpBB3/app.php/news
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
hanelyp
Registered User
Posts: 124
Joined: Wed Apr 02, 2014 10:20 pm

Re: [HowTo] Convert 3.0 MOD into 3.1 Extension

Post by hanelyp »

Trying to port my jigsaw puzzle captcha from 3.0 to 3.1. The spambot countermeasures page of ACP bombs when trying to process my code. Neither $phpbb_root_path nor PHPBB_ROOT_PATH is properly defined, and I need it to include a second php file as part of the extension.

Return to “Extension Writers Discussion”