J3.x

J3.x:تطوير مكون MVC/اضافة عرض الى قسم الموقع

From Joomla! Documentation

< J3.x:Developing an MVC Component
This page is a translated version of the page J3.x:Developing an MVC Component/Adding a view to the site part and the translation is 95% complete.
Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎português do Brasil • ‎русский • ‎العربية • ‎中文(台灣)‎
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.

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



ملاحظات

  • يمكنك اتباع الخطوات في الأسفل لإنشاء مكون Hello World!,أو يمكنك مباشرة تحميل الرابط archive
  • As you follow through the steps of the tutorial you may find problems. In this case you may find it useful to read the Joomla page on How to debug your code.

اضافة عرض الى Hello World!

في هذه المقالة سنغطي كيفية اضافة عرض الى مكون Joonla! البسيط. لهذا المثال سنتابع عملنا على مكون Hello World!

هنالك عدة طرق لتحديث مكون Joomla!. في هذا الدرس سنسلط الضوء على الخيار الثاني.

1 اضافة ملفات يدويا الى <path_to_joomla>/
2 التحديث باستخدام مدير مكون Joomla! والفهرس الأصلي، غير مضغوط، المستخدم لتثبيت المكون.
3 التحديث باستخدام مدير مكون Joomla! و مخدم التحديث

لاضافة عرض يجب عليك الانتقال الى com_helloworld, والذي هو الفهرس الأصلي الذي أنشأناه لمكوننا. باستخدام مدير الملف المفضل لديك، أنشئ أو حدث الملفات التالية، ومع التحديث أو الانشاء للملفات ، أضف شيفرة المصدر لكل ملف وهو موجود في File Details.

1 تحديث: helloworld.php <path_to_com_helloworld>/site/helloworld.php
2 انشاء: controller.php <path_to_com_helloworld>/site/controller.php
3 انشاء: index.html <path_to_com_helloworld>/site/views/index.html
4 انشاء: index.html <path_to_com_helloworld>/site/views/helloworld/index.html
5 انشاء: view.html.php <path_to_com_helloworld>/site/views/helloworld/view.html.php
6 انشاء: default.php <path_to_com_helloworld>/site/views/helloworld/tmpl/default.php
7 انشاء: index.html <path_to_com_helloworld>/site/views/helloworld/tmpl/index.html
8 تحديث: helloworld.xml <path_to_com_helloworld/helloworld.xml

تحديث مكون Hello World!

لتحديث مكون Hello World! الموجود في موقع Joomla!، الرجاء اتباع نفس الخطوات في التثبيت الأصلي.


تفاصيل الملف

site/helloworld.php (Note that some browsers may not copy and paste highlighted text correctly into a text editor. If you find this, then please try a different browser).

<?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
$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

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

site/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');
/**
 * Hello World Component Controller
 *
 * @since  0.0.1
 */
class HelloWorldController extends JControllerLegacy
{
}

site/views/helloworld/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');

/**
 * HTML View class for the HelloWorld Component
 *
 * @since  0.0.1
 */
class HelloWorldViewHelloWorld 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)
	{
		// Assign data to the view
		$this->msg = 'Hello World';

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

site/views/helloworld/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');
?>
<h1><?php echo $this->msg; ?></h1>

index.html ملاحظة - الشفرة نفسها تستخدم لكافة الفهارس

<html><body bgcolor="#FFFFFF"></body></html>

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.2</version>
	<!-- The description is optional and defaults to the name -->
	<description>Description of the Hello World component ...</description>

	<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>
	</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>
			<!-- SQL files section -->
			<folder>sql</folder>
		</files>
	</administration>

</extension>


شرح الشفرة

في حال كنت مستغرب لماذا يعمل بهذه الطريقة.

helloworld.php

defined('_JEXEC') or die('Restricted access');

هذا يسمح بنقطة دخول أمنة الى منصة Joomla!.يحتوي JEXEC على شرح تفصيلي.

$controller = JControllerLegacy::getInstance('HelloWorld');

إن JControllerLegacy هي الصنف الأساسي لموجه Joomla!. ولكي يستخدم الموقع الخاص بنا الموجهات، يجب توسيع هذا الصنف في مكوننا. الطريقة الثابتة getInstance للصنف JControllerLegacy سينشئ الموجه. في الشيفرة فوق، سيتم انشاء مثيل كائن موجه من صنف يدعى HelloWorldController. ستبحث Joomla! عن تعريف هذا الصنف في <path_to_joomla>/htdocs/components/com_helloworld/controller.php.

$input = JFactory::getApplication()->input;
$controller->execute($input->getCmd('task'));

بعد انشاء الموجه، سنرشد الموجه لتنفيذ المهمة، كما هو محدد في URL: <yoursite>/joomla/index.php?option=com_helloworld&task=<task_name>. واذا لم يتم تحديد المهمة، سيتم اعتبارها المهمة الافتراضية display'. عند استخدام display ، سيقرر متغير ال"عرض" ما سيتم عرضه. المهام الأخرى المعروفة هي حفظ، تعديل، جديد، الخ.

$controller->redirect();

ربما يقرر الموجه اعادة توجيه الصفحة، عادة بعد اكتمال مهمة مثل 'حفظ'. إن آخر تعليمة هي من يأخذ على عاتقه التوجية الفعلي.

نفطة الدخول الرئيسية، , helloworld.php, يمر أساسا تحكم إلى الموجه، الذي يعالج تنفيذ المهمة الذي تم تحديده في الطلب. موجه مكوننا المحدد لا تفعل شيئا أكثر مما يفعله الصنف الأصل، ولهذا السبب فإن صنف الموجه الخاص بنا فارغ.

controller.php

class HelloWorldController extends JControllerLegacy
{
}

عندما لايوجد مهمة في متغير الطلب، فإن المهمة الافتراضية ستنفذ. وهي مهمة العرض Display افتراضيا. والصنف JControllerLegacy يملك مثل هذه المهمة. في مثالنا، سيتم عرض العرض المسمى HelloWorld.

view.html.php

class HelloWorldViewHelloWorld extends JViewLegacy
{
	function display($tpl = null)
	{
		// Assign data to the view
		$this->msg = 'Hello World';
 
		// Display the view
		parent::display($tpl);
	}
}

العرض يجهز النص الذي سيتم اخراجه ومن ثم يستدعي الصنف الأساسي display . JViewLegacy هو الصنف الأساسي لعرض Joomla!. وفي حالتنا، هذه الطريقة ستعرض البيانات باستخدام ملف tmpl/default.php

Caveat: If your view needs to load or otherwise embed individual JavaScript code, do not do that in the view, as the code might not get included when caching is enabled. Load those scripts in the controller instead. See the related issue on the issue tracker.

default.php

<h1><?php echo $this->msg; ?></h1>

ملف القالب هذا سيتضمن بصنف JViewLegacy . ولذلك، هنا، تشير $this الى HelloWorldViewHelloWorld class.

helloworld.xml

<version>0.0.2</version>

حدث رقم النسخة

<filename>controller.php</filename>
<folder>views</folder>

أخبر تطبيق المثبت لاضافة controller.php و فهرس views

محتويات المكون

في هذه النقطة من الدرس، المكون الخاص بك يجب أن يملك الملفات التالية:

1 helloworld.xml هذا ملف XML (manifest) والذي يخبر Joomla! عن كيفية تثبيت المكون الخاص بنا.
2 site/helloworld.php وهذا هو نقطة دخول الموقع الى مكون Hello World!
3 site/index.html منع مخدم الويب من عرض محتوى الفهرس
4 site/controller.php ملف يمثل الموجه
5 site/views/index.html منع مخدم الويب من عرض محتوى الفهرس
6 site/views/helloworld/index.html منع مخدم الويب من عرض محتوى الفهرس
7 site/views/helloworld/view.html.php ملف يمثل عرض
8 site/views/helloworld/tmpl/index.html منع مخدم الويب من عرض محتوى الفهرس
9 site/views/helloworld/tmpl/default.php عرض افتراضي
10 admin/index.html منع مخدم الويب من عرض محتوى الفهرس
11 admin/helloworld.php وهذا هو نقطة دخول المسؤول الى مكون Hello World!
12 admin/sql/index.html منع مخدم الويب من عرض محتوى الفهرس
13 admin/sql/updates/index.html منع مخدم الويب من عرض محتوى الفهرس
14 admin/sql/updates/mysql/index.html منع مخدم الويب من عرض محتوى الفهرس
15 admin/sql/updates/mysql/0.0.1.sql ملف يسمح لتهيئة اصدار مخطط مكون com_helloworld
Info non-talk.png
General Information

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