J3.x

J3.x:تطوير مكون MVC/خلفية بسيطة

From Joomla! Documentation

< J3.x:Developing an MVC Component
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
This page is a translated version of the page J3.x:Developing an MVC Component/Basic backend and the translation is 94% complete.
Other languages:
English • ‎Nederlands • ‎español • ‎français • ‎العربية • ‎中文(台灣)‎
Joomla! 
3.x
درس
تطوير مكون MVC

اضافة طلب متحول في نوع القائمة

استخدام قاعدة البيانات

واجهة خلفية بسيطة

اضافة ادارة لغة

اضافة أفعال في الواجهة الخلفية

اضافة ديكور الى الواجهة الخلفية

اضافة التحقيقات

اضافة فئات

اضافة اعداد

  1. اضافة لائحة تحكم بالوصول ACL

اضافة ملف سكريبت لتثبيت/فك تثبيت/تحديث

Adding a Frontend Form

  1. Adding an Image
  2. Adding a Map
  3. Adding AJAX
  4. Adding an Alias

استخدام ميزات تصفية اللغة

  1. Adding a Modal
  2. Adding Associations
  3. Adding Checkout
  4. Adding Ordering
  5. Adding Levels
  6. Adding Versioning
  7. Adding Tags
  8. Adding Access
  9. Adding a Batch Process
  10. Adding Cache
  11. Adding a Feed

اضافة مخدم تحديث

  1. Adding Custom Fields
  2. Upgrading to Joomla4



هذه سلسلة من عدة مقالات من الدروس حول كيفية تطوير موديل-عرض-موجه مكون لنسخة Joomla! Joomla 3.x.

تبدأ مع مقدمة, وتستعرض المقالات في هذه السلسلة باستخدام زر التنقل في الأسفل أو الصندوق الأيمن ("المقالات في هذه السلسلة").



مقدمة

هذا الدرس هو جزء من درس تطوير مكون MVC لـ Joomla! 3.2 . نشجعك على قراءة الجزء السابق من الدرس قبل قراءة هذا الدرس.

There are 2 videos associated with this step in the tutorial, covering MVC Structure and Pagination, and HTML elements and Javascript code.

الخلفية البسيطة

تصميم شاشة الخلفية يقودنا على الأقل الى ثلاثية Model-View-Controller. يجب علينا تعديل نقطة دخول المدير لمكوننا، ملف admin/helloworld.php

admin/helloworld.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// Get an instance of the controller prefixed by HelloWorld
$controller = JControllerLegacy::getInstance('HelloWorld');

// Perform the Request task
$controller->execute(JFactory::getApplication()->input->get('task'));

// Redirect if set by the controller
$controller->redirect();

انشاء وتوليد الموجه

نقطة الدخول الآن تحصل على instance من 'HelloWorld قيل الموجه. دعنا ننشئ الموجه الرئيسي لقسم المدير:

admin/controller.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * General Controller of HelloWorld component
 *
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 * @since       0.0.7
 */
class HelloWorldController extends JControllerLegacy
{
	/**
	 * The default view for the display method.
	 *
	 * @var string
	 * @since 12.2
	 */
	protected $default_view = 'helloworlds';
}

سيعرض الموجه عرض 'HelloWorlds' بشكل افتراضي.

انشاء العرض

باستعمال مدير الملف المفضل والمحرر أنشئ ملف admin/views/helloworlds/view.html.php يحتوي

admin/views/helloworlds/view.html.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * HelloWorlds View
 *
 * @since  0.0.1
 */
class HelloWorldViewHelloWorlds extends JViewLegacy
{
	/**
	 * Display the Hello World view
	 *
	 * @param   string  $tpl  The name of the template file to parse; automatically searches through the template paths.
	 *
	 * @return  void
	 */
	function display($tpl = null)
	{
		// Get data from the model
		$this->items		= $this->get('Items');
		$this->pagination	= $this->get('Pagination');

		// Check for errors.
		if (count($errors = $this->get('Errors')))
		{
			JError::raiseError(500, implode('<br />', $errors));

			return false;
		}

		// Display the template
		parent::display($tpl);
	}
}

في Joomla!، تُظهر العروض (views) البيانات باستخدام تصميم(layout). باستعمال مدير الملف المفضل والمحرر ضع ملف admin/views/helloworlds/tmpl/default.php يحتوي

admin/views/helloworlds/tmpl/default.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted Access');
?>
<form action="index.php?option=com_helloworld&view=helloworlds" method="post" id="adminForm" name="adminForm">
	<table class="table table-striped table-hover">
		<thead>
		<tr>
			<th width="1%"><?php echo JText::_('COM_HELLOWORLD_NUM'); ?></th>
			<th width="2%">
				<?php echo JHtml::_('grid.checkall'); ?>
			</th>
			<th width="90%">
				<?php echo JText::_('COM_HELLOWORLD_HELLOWORLDS_NAME') ;?>
			</th>
			<th width="5%">
				<?php echo JText::_('COM_HELLOWORLD_PUBLISHED'); ?>
			</th>
			<th width="2%">
				<?php echo JText::_('COM_HELLOWORLD_ID'); ?>
			</th>
		</tr>
		</thead>
		<tfoot>
			<tr>
				<td colspan="5">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
		</tfoot>
		<tbody>
			<?php if (!empty($this->items)) : ?>
				<?php foreach ($this->items as $i => $row) : ?>

					<tr>
						<td>
							<?php echo $this->pagination->getRowOffset($i); ?>
						</td>
						<td>
							<?php echo JHtml::_('grid.id', $i, $row->id); ?>
						</td>
						<td>
							<?php echo $row->greeting; ?>
						</td>
						<td align="center">
							<?php echo JHtml::_('jgrid.published', $row->published, $i, 'helloworlds.', true, 'cb'); ?>
						</td>
						<td align="center">
							<?php echo $row->id; ?>
						</td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
		</tbody>
	</table>
</form>

COM_HELLOWORLD_HELLOWORLDS_NAME, COM_HELLOWORLD_ID والباقي أمكنة محجوزة سيتم تبديلها فيما بعد بنص محدد اللغة. طريقة JText::_ تترجم سلسلة الأحرف الى اللغة الحالية.

checkAllهو تابع جافا سيكريبت يحدد في نواة Joomla! قادر على تحديد كل العناصر.

JHtml::_هو تابع مساعدة قادر على اظهار عدة خرج HTML . وفي هذه الحالة، سيُظهر مربع خيار للعنصر.

JPagination هو صنف لـ Joomla! قادر على ادارة واظهار كائن ترقيم الصفحات.

انشاء الموديل

يسأل عرض الـ HelloWorlds الموديل عن البيانات. في Joomla!، يوجد صنف قادر على ادارة لائحة من البيانات: JModelList. صنف JModelList وكل الأصناف الموروثة تحتاج الى method واحدة فقط:

  • getListQuery والذي يبني استعلام SQL

و حالتين:

  • list.start لتحديد بداية اللائحة
  • list.limitلتحديد طول اللائحة

إن getItems و getPagination methods محددين في صنف JModelList . ولاحاجة لاعادة تعريفهم في صنف HelloWorldModelHelloWorlds .

admin/models/helloworlds.php

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

/**
 * HelloWorldList Model
 *
 * @since  0.0.1
 */
class HelloWorldModelHelloWorlds extends JModelList
{
	/**
	 * Method to build an SQL query to load the list data.
	 *
	 * @return      string  An SQL query
	 */
	protected function getListQuery()
	{
		// Initialize variables.
		$db    = JFactory::getDbo();
		$query = $db->getQuery(true);

		// Create the base select statement.
		$query->select('*')
                ->from($db->quoteName('#__helloworld'));

		return $query;
	}
}

The _populateState method is, بشكل افتراضي, تُستدعى عندما تكون الحالة جاهزة بواسطة getState method.

حزم المكون

محتوياات فهرس الشفرة الخاص بك

انشئ ملف مضغوط عن هذا الفهرس أو حمل مباشرة من archive وثبته باستخدام مدير الملحقات في Joomla!. بامكانك اضافة عنصر قائمة لهذا المكون باستخدام مدير القائمة في الخلفية.

helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">

	<name>Hello World!</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>January 2018</creationDate>
	<author>John Doe</author>
	<authorEmail>john.doe@example.org</authorEmail>
	<authorUrl>http://www.example.org</authorUrl>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<!--  The version string is recorded in the components table -->
	<version>0.0.7</version>
	<!-- The description is optional and defaults to the name -->
	<description>Description of the Hello World component ...</description>

	<install> <!-- Runs on install -->
		<sql>
			<file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
		</sql>
	</install>
	<uninstall> <!-- Runs on uninstall -->
		<sql>
			<file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>
		</sql>
	</uninstall>
	<update> <!-- Runs on update; New since J2.5 -->
		<schemas>
			<schemapath type="mysql">sql/updates/mysql</schemapath>
		</schemas>
	</update>

	<!-- Site Main File Copy Section -->
	<!-- Note the folder attribute: This attribute describes the folder
		to copy FROM in the package to install therefore files copied
		in this section are copied from /site/ in the package -->
	<files folder="site">
		<filename>index.html</filename>
		<filename>helloworld.php</filename>
		<filename>controller.php</filename>
		<folder>views</folder>
		<folder>models</folder>
	</files>

	<administration>
		<!-- Administration Menu Section -->
		<menu link='index.php?option=com_helloworld'>Hello World!</menu>
		<!-- Administration Main File Copy Section -->
		<!-- Note the folder attribute: This attribute describes the folder
			to copy FROM in the package to install therefore files copied
			in this section are copied from /admin/ in the package -->
		<files folder="admin">
			<!-- Admin Main File Copy Section -->
			<filename>index.html</filename>
			<filename>helloworld.php</filename>
			<filename>controller.php</filename>
			<!-- SQL files section -->
			<folder>sql</folder>
			<!-- tables files section -->
			<folder>tables</folder>
			<!-- models files section -->
			<folder>models</folder>
			<!-- views files section -->
			<folder>views</folder>
		</files>
	</administration>

</extension>

يمكنك الآن أن ترى في مكونك hello-world مصفوفة بعمودين، وسطرين وخانات اختيار. يمكنك الضغط على خانات الاختيار لتحدد الخيارات المختلفة التي تريدها.

Info non-talk.png
General Information

الرجاء انشاء طلب سحب أو مشكلة على https://github.com/joomla/Joomla-3.2-Hello-World-Component لأي اختلافات في الكود أو أي تعديل في شفرة المصدر على هذه الصفحة.

Contributors