Skip to main content

الدرس العاشر: تسمية التوجيهات Named Routes


الجزء 10: Named Routes

في الجزء ال9 تعلمنا استخدام الRoute Resource ومدى اهميته في أختصار كتابة التوجيهات في ملف الroutes.php هذا الدرس مكمل حيث سأتطرق الى استخدام الNamed Routes وما فائدتها وكيفية استخدامها وسأستعمل من ال illuminate/html package الملف helper.php الذي يحتوي على دوال تختصر كتابة اكواد الhtml:

لنأخذ سيناريو حول استخدام الNamed Routes:
اذا أراد مديرك تغيير رابط عرض الأخبار من news الى events فما تقديرك للوقت الذي ستستنفذه في تغيير كل الروابط في الموقع من news/ و /news/{slug} الى events/ و /events/{event} ?  
يمكنك فعل ذلك ولكن بعد عناءً طويل................. فلماذا ذلك حيث عن طريق سطر برمجي يقيكَ من كل ذلك التعب


لندخل الى تطبيق ذلك: فلنعد الى الواجهة index.blade.php: كما ستلاحظ عند انشاء الرابط ادخلنا عنوان الرابط يدوياً!
واذا غيرنا التوجيه من
news الى events في ملف الroutes.php فيجب علينا القدوم هنا وتغيير عنوان الرابط مجدداً؟!
@extends('layout')
@section('content')
<h1>News</h1>
<ul>
@foreach($news as $event)
<li><a href="/news/{{ $event->slug }}">{{ $event->title }}</a></li>
@endforeach
</ul>
@stop

الحل بالعودة الى ملف الroutes.php: وتعديل التوجيه للIndex واضافة اسم لهذا التوجيه حيثما تغير التوجيه يبقى اسمه ثابتاً!
التوجيه القديم بدون تعيين اسم لهذا التوجيه:
get('news','NewsController@index');

التوجيه القديم مع تعيين اسم لهذا التوجيه: اسم التوجيه news_path وسيستخدم uses  الController الذي اسمه   NewsController@index:
get('news',
['as'=>'news_path','uses'=>'NewsController@index']);

التوجيه الجديد مع تعيين اسم لهذا التوجيه: نفس اسم الكود اعلاه:
get('events', ['as'=>'news_path','uses'=>'NewsController@index']);


سأغير توجيه عرض حدث معين كالأتي:
get('events/{event}',['as'=>'event_path','uses'=>'NewsController@show']);

Route::bind('event',function($slug){
    return App\news::where('slug',$slug)->first();
});



ولكن عند تصفح الأحداث سيظهر خطأ عدم وجود توجيه /news/{slug} لأن الواجهة index.blade.php لاتزال تشير الى news سأغيرها بطريقة ديناميكية بالأشارة الى اسم التوجيه وليس للتوجيه نفسه:

@extends('layout')
@section('content')
<h1>News</h1>
<ul>
@foreach($news as $event)
<li><a href="{{ route('event_path',[$event->slug]) }}">{{ $event->title }}</a></li>
@endforeach
</ul>
@stop
لأحظ داخل الرابط استخدمت الدالة route(‘{name_of_route}’,[variable parameters]).

ملاحظة:والأن يمكنك تغيير التوجيه لأي توجيه تريده لأننا حددنا اسم ذلك التوجيه وأستخدمناه بدل التوجيه نفسه فقط اسمه!


كذلك تعلمنا في الجزء الثامن كيف نستخدم الForm Component عن طريق ادراج الPackage التالي :
Illuminate/html
يوجد ضمن هذا الPackage ملف اسمه helpers.php يوجد داخل هذا الملف دوال تفيد في اختصار كتابة الCodes داخل الواجهات مثلاً في الواجهة index.blade.php يمكن اختصار كتابة الرابط كالتالي:

@extends('layout')
@section('content')
<h1>News</h1>
<ul>
@foreach($news as $event)
<li>
{!! Link_to_route(‘event_path’,$event->title,[$event->slug]) !!}
</li>
@endforeach
</ul>
@stop

أول قيمة لدالة link_to_route هي اسم التوجيه وثاني قيمة عنوان الرابط وثالث قيمة المتغيرات التي ستمرر مع اسم التوجيه.


أضافة الروابط بنفس الطريقة في الواجهة show.blade.php:
@extends('layout')
@section('content')
<h1>{{ $news->title }}</h1>
@if($news->content)
<article class="news_content">{{ $news->content }}</article>
@endif
{!! link_to_route('news_path','Back to Home') !!}
{!! link_to_route('edit_path','Edit this event',[$news->slug]) !!}
@stop

لاحظ في رابط تعديل الحدث تم التوجيه مع اسم التوجيه edit_path لنضف ذلك التوجيه في ملف الroutes.php:
get('events/{event}/edit',['as'=>'edit_path','uses'=>'NewsController@edit']);


دعنا لاننسى ال Form في صفحة تعديل الحدث  edit.blade.phpلتعديل التوجيه عند الضغط على Submit:
@extends(‘layout’)
@section(‘content’)
<h1>Welcome to Edit Page</h1>
<h1>{{ $news->title }}</h1>
{!! Form::model($news,[‘url’=>route(‘update_path’,[$news->slug]),’method’=>’PATCH’]) !!}
{!! Form::text(‘title’,null,[‘class’=>’form_txt’]) !!}
{!! Form::textarea(‘content’,null,[‘class’=>’form_txtA’]) !!}
{!! Form::submit(‘Update’) !!}
{!! Form::close() !!}
<p>{{ $news->content }}</p>
@stop

اضافة توجيه صحيح للForm اعلاه في ملف الroutes.php :
patch(‘events/{event}/edit’,[‘as’=>’update_path’,’uses’=>’NewsController@update’]);




وأيضاً في دالة الupdate في ملف الNewsController عند انتهاء تعديل الى اين يتم تحويلك ذلك التحويل يجب ان يعدل ايضاً الى اسم توجيه عرض كافة الأحداث والذي هو news_path:
    public function update(news $news, Request $request)
    {
        $news->fill($request->input())->save();
        return redirect(route('news_path'));
    }


الملف النهائي للroutes.php يصبح كالتالي:
get('events', ['as'=>'news_path','uses'=>'NewsController@index']);
get('events/{event}',['as'=>'event_path','uses'=>'NewsController@show']);
get('events/{event}/edit',['as'=>'edit_path','uses'=>'NewsController@edit']);
patch('events/{event}/edit',['as'=>'update_path','uses'=>'NewsController@update']);
Route::bind('event',function($slug){
    return App\news::where('slug',$slug)->first();
});




تعلمنا في الجزء السابق من اجل كتابة توجيه واحد يسمى بCollection يشمل كافة انواع التوجيهات الأفتراضية عن طريق استخدام الRoute Resource


والأن اريد اختصار التوجيهات اعلاه بأستخدام دالة برمجية واحدة وهي resource كالتالي:
$router->resource('events','NewsController',[
'names' => ['index'=>'news_path','show'=>'event_path','edit'=>'edit_path','update'=>'update_path'],
'only'  => ['index','show','edit','update']
]
);
Route::bind('events',function($slug){
    return App\news::where('slug',$slug)->first();
});


Comments

Popular posts from this blog

PART #2: Registration and Login System Tutorial Using PHP and MYSQL

PART #2: Registration and Login System Tutorial Using PHP and MYSQL Registration and login system using PHP and MySQL database, in this part you will do the following: 1- Creating the registration page. 2- Validating the registration form. 3- Sending the activation link on successful registration. note: you can find the mailserver tool here: [MailServerTool] Good luck. PHP,Mysql,Programming,web development,How to,Technology,web programming,web project ideas,safaa al-hayali,saf3al2a,Registration and login,Registration and login in php,php and mysql login system,registration system using php and mysql,login and register php,login and register php with database,database,php tutorial,learn php,tutorial

How To Play .srt Subtitles File in Windows Media Player

How To Play .srt Subtitles File in Windows Media Player links: https://sourceforge.net/projects/wmpsub/ wmp,.srt Subtitles File in Windows Media Player,How To Play .srt Subtitles File in Windows Media Player,How To,Safaa Al-Hayali,saf3al2a,srt subtitle in WMP,Windows,Windows Media Player,media,.ass,local subtitles

Virtual Box : How to Increase Disk Size - Windows

How to increase disk size or disk storage in oracle virtual box the command you have to use: vboxmanage.exe modifymedium "[YourPathToVdiFileOfYourVirtualMachine]" --resize [NUMBER] virtual,box,vbox,oracle,partition,size,disk,increase,resize,windows,10,microsoft,vdi,vboxmanage,configuration,Safaa AL-Hayali,saf3al2a,Oracle,VirtualBox,How to,windows 10