Routing در لاراول مسیریابی

ساخت وبلاگ

Routing 
در لاراول مسیریابی، نقش مهم در کارایی هسته هر قاب ورک MVC ایفا می کند . در راستی Route یه نگاشت دربین موتور requests و response می گویند و درصورتی که بخواهیم خیلی معمولی آن را تعریف کنیم آدرس های URL ای که شما در مرورگرتان وارد می نمائید با route مدیر می شود و نقاط محل ورود برنامه را تعریف می کند . داشتن یک routing انعطاف پذیر می تواند به شما امداد نماید تا در دست گرفتن خوبتر و کاربردی تر را در برنامه تان داشته باشید . 

طراحی سایت در مشهد

لاراول دارنده یک مسیریابی حاذق می باشد که بر اساس مسیریابی Symfony نوشته شده و فعالیت با آن بسیار سهل و آسان هست . همینطور رابط کاربری سهل وآسان و امکان های زیادی داراست که برنامه نویسی با آن را برای شما لذت بخش خیس می نماید . 

تعریف 
Route ها 
در لاراول Route های برنامه ما در مسیر apphttproutes .php توصیف می شوند . یک نمونه ساده از فایل routes .php 

// will be used to handle GET requests . 
Route::get('index',function() 

echo 'this is index page'; }); 

Route::get('login',function() 

echo 'GET login requests will be hndled here .'; 
}); 

// will be used to handle POST requests . 
Route::post('login', 
function() { 
echo 'POST login requests will be handled here .'; 
}); 









10 
11 
12 
13 
14 
15 
16 
17 
18 

// will be used to handle GET requests . 
Route::get('index',function() 

echo 'this is index page'; }); 

Route::get('login',function() 

echo 'GET login requests will be hndled here .'; 
}); 

// will be used to handle POST requests . 
Route::post('login', 
function() { 
echo 'POST login requests will be handled here .'; 
}); 
ما 
همینطور می توانیم از متد کنترل ها استفاده کنیم مانند زیر : 
Route::get('users', 
'UsersController@getIndex'); 1 

Route::get('users', 
'UsersController@getIndex'); در کد بالا هنگامی که ما درخواستی بصورت /users داشته باشیم این درخواست بوسیله route به متد getIndex از در اختیار گرفتن UserController متصل می شود و داده ها را به نسبت کدی که در getIndex قرار دارد به استفاده کننده نشان می دهد . همچنین می توانیم داده ها را بطور بدون واسطه توسط افعال http (با به کارگیری از Route::put و Route::delete) حذف یا این که بیشتر کنیم . 

پارامتر 
ها در Route 

// parameter {id} will be passed to the closure . 
Route::any('post/{id}',function($id) 

echo "post with id: $id"; }); 


// A model with given post id will be passed to closure for any HTTP request . 
Route::any('post/{post}',function($post) 

echo "post with id: $post->id"; }); 









10 
11 
12 
13 
14 

// parameter {id} will be passed to the closure . 
Route::any('post/{id}',function($id) 

echo "post with id: $id"; }); 


// A model with given post id will be passed to closure for any HTTP request . 
Route::any('post/{post}',function($post) 

echo "post with id: $post->id"; }); 
نکته : filter که در نسخه ۴ لاراول در route ها قضیه به کارگیری قرار می گرفت در نسخه ۵ جای خویش را به Middleware داده میباشد . 

می 
توانیم در هر route با توصیف کلید در آرایه ای که در زیر می بینید یک نام روتر دوم داشته باشیم : 

Route::get('admin',['as'=>'admin .home','middleware' 
=> 'auth',function() { 

retu 'is already called'; }]); 

//another example using controller action . 
Route::get('/post/list',['as' => 'post .list','uses' 
=> 'PostController@listPost']); 1 







Route::get('admin',['as'=>'admin .home','middleware' 
=> 'auth',function() { 

retu 'is already called'; }]); 

//another example using controller action . 
Route::get('/post/list',['as' => 'post .list','uses' 
=> 'PostController@listPost']); در 
view ها نیز می توانیم با به کار گیری از route() مسیرهای جانور را به لینک قابل کلیک تبدیل کنیم بصورت تحت : 
route('post .list'); 


route('post .list'); 
شما می توایند برای توضیحات بی نقص تر در موضوع route ها به اسناد خود لاراول مراجعه نمایید . 

تولید 
Route های مسئله نیاز برای وبلاگ 
در 
تحت مسیرهایی که برای برنامه مان استفاده می کنیم را می توانید ملاحظه کنید : 
//file: app/http/routes .php 

Route::controllers([ 'auth' 
=> 'AuthAuthController', 'password' 
=> 'AuthPasswordController', ]); 

/* 
User routes */ get('/post/{post}/show', ['as' => 'post .show', 
'uses' => 'PostController@showPost']); post('/post/{post}/comment', ['as' => 'comment .new', 
'uses' => 'CommentController@newComment']); 
/* 
Admin routes */ Route::group(['prefix' 
=> 'admin', 'middleware' => 'auth'], function () { 
/*get routes*/ 
get('dash-board', function () { 
$useame = Auth::user()->name; 
retu view('dash')->with('content', "Hi $useame, Welcome to Dashboard!") 
->withUseame('useame',$useame) 
->withTitle('title','DashBoard'); 
}); get('/post/list', ['as' => 'post .list', 
'uses' => 'PostController@listPost']); get('/post/new', ['as' => 'post .new', 
'uses' => 'PostController@newPost']); get('/post/{post}/edit', ['as' => 'post .edit', 
'uses' => 'PostController@editPost']); get('/post/{post}/delete', ['as' => 'post .delete', 
'uses' => 'PostController@deletePost']); get('/comment/list', ['as' => 'comment .list', 
'uses' => 'CommentController@listComment']); get('/comment/{comment}/show', ['as' => 'comment .show', 
'uses' => 'CommentController@showComment']); get('/comment/{comment}/delete', ['as' => 'comment .delete', 
'uses' => 'CommentController@deleteComment']); 

/*post routes*/ post('/post/save', ['as' => 'post .save', 
'uses' => 'PostController@savePost']); post('/post/{post}/update', ['as' => 'post .update', 
'uses' => 'PostController@updatePost']); post('/comment/{comment}/update', ['as' => 'comment .update', 
'uses' => 'CommentController@updateComment']); 
}); 

/* 
Home routes */ Route::controller('/', 
'BlogController'); 
/* 
View Composer */ View::composer('sidebar', 
function ($view) { 
$view->recentPosts = Apppost::orderBy('id', 'desc')->take(5)->get(); }); 









10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
//file: app/http/routes .php 

Route::controllers([ 'auth' 
=> 'AuthAuthController', 'password' 
=> 'AuthPasswordController', ]); 

/* 
User routes */ get('/post/{post}/show', ['as' => 'post .show', 
'uses' => 'PostController@showPost']); post('/post/{post}/comment', ['as' => 'comment .new', 
'uses' => 'CommentController@newComment']); 
/* 
Admin routes */ Route::group(['prefix' 
=> 'admin', 'middleware' => 'auth'], function () { 
/*get routes*/ 
get('dash-board', function () { 
$useame = Auth::user()->name; 
retu view('dash')->with('content', "Hi $useame, Welcome to Dashboard!") 
->withUseame('useame',$useame) 
->withTitle('title','DashBoard'); 
}); get('/post/list', ['as' => 'post .list', 
'uses' => 'PostController@listPost']); get('/post/new', ['as' => 'post .new', 
'uses' => 'PostController@newPost']); get('/post/{post}/edit', ['as' => 'post .edit', 
'uses' => 'PostController@editPost']); get('/post/{post}/delete', ['as' => 'post .delete', 
'uses' => 'PostController@deletePost']); get('/comment/list', ['as' => 'comment .list', 
'uses' => 'CommentController@listComment']); get('/comment/{comment}/show', ['as' => 'comment .show', 
'uses' => 'CommentController@showComment']); get('/comment/{comment}/delete', ['as' => 'comment .delete', 
'uses' => 'CommentController@deleteComment']); 

/*post routes*/ post('/post/save', ['as' => 'post .save', 
'uses' => 'PostController@savePost']); post('/post/{post}/update', ['as' => 'post .update', 
'uses' => 'PostController@updatePost']); post('/comment/{comment}/update', ['as' => 'comment .update', 
'uses' => 'CommentController@updateComment']); 
}); 

/* 
Home routes */ Route::controller('/', 
'BlogController'); 
/* 
View Composer */ View::composer('sidebar', 
function ($view) { 
$view->recentPosts = Apppost::orderBy('id', 'desc')->take(5)->get(); }); 
در کد بالا برای اعتبار سنجی مدیر از کنترل auth به کارگیری می کنیم و همچنین در route های بعدی یعنی post .show و comment .new ما به مخاطب ها اذن می دهیم تا پست ها را در صفحات تکی (single page) ببینند و در صورتیکه خواستند نظر خود را در مسئله آن پست ارسال کنند . در route بعدی یک route:group می سازیم که تمام مسیر های مربوط به مدیریت در آن قرار می گیرد و یک پیشوند admin نیز برای route هایی که در این مجموعه قرار دارا هستند توصیف می کنیم تا هر route که درخواست شد قبلی از آن admin بیابد . برای مثال وقتی که مسیر post .edit فراخوانی شود به url به این چهره بازگردانی می شود . 
(http://localhost/admin/post/۱۲/edit)

مرجع مقالات رسمی طراحی سایت...
ما را در سایت مرجع مقالات رسمی طراحی سایت دنبال می کنید

برچسب : طراحی سایت در مشهد, نویسنده : علی پور web01 بازدید : 606 تاريخ : سه شنبه 7 خرداد 1398 ساعت: 15:22