Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [inny][Laravel] Routes
Randallmaster
post
Post #1





Grupa: Zarejestrowani
Postów: 677
Pomógł: 11
Dołączył: 18.11.2009

Ostrzeżenie: (0%)
-----


1 pytanie:

mam stworzonego routa:

Route::resources('users', 'UsersController')

jak teraz zmienić w metodzie index() (UsersController@index) aby dodać do niej parametr?

muszę wszystkie metody z UsersController wypisać

Route::get('users/{param?}', 'UsersController');
itd...

czy można to jakoś wstrzyknąć?

2 pytanie:

mam widok z parametrem

/users/1

czy jest możliwość ustawienia parametru null? aby po wejście na stronę /users nie wywalało mi błędu z brakującym parametrem.


Go to the top of the page
+Quote Post
Pyton_000
post
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


1. Dodaj poniżej tylko zmieniany route np:
Kod
Route::get('users/{param?}', 'UsersController');

2. w metodzie musisz dać parametr = null

Ten post edytował Pyton_000 25.08.2015, 15:25:53
Go to the top of the page
+Quote Post
Randallmaster
post
Post #3





Grupa: Zarejestrowani
Postów: 677
Pomógł: 11
Dołączył: 18.11.2009

Ostrzeżenie: (0%)
-----


Cytat(Pyton_000 @ 25.08.2015, 16:25:35 ) *
1. Dodaj poniżej tylko zmieniany route np:
Kod
Route::get('users/{param?}', 'UsersController');


- Jak dodam tak :
Kod
Route::get('users/{param?}', 'UsersController');

Błąd: Invalid route action:

- Wydawało mi się ze chodziło ci o:
Kod
Route::get('users/{param?}', 'UsersController@index');

Ale też występuje błąd ponieważ miesza się z linkami taki jak:
UsersController@show która korzysta z users/{param?}

Ten post edytował Randallmaster 25.08.2015, 16:58:34
Go to the top of the page
+Quote Post
Malukaz
post
Post #4





Grupa: Zarejestrowani
Postów: 77
Pomógł: 4
Dołączył: 16.04.2009

Ostrzeżenie: (10%)
X----


podpinam sie pod pytanie twórcy tematu.
Go to the top of the page
+Quote Post
memory
post
Post #5





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

Ostrzeżenie: (0%)
-----


  1. Route::get('users/{param?}', 'UsersController@index');

następnie
  1. class UsersController {
  2.  
  3. public function index($param = null){
  4.  
  5. }
  6. }


Czytaj dokładnie pyton ci napisał w metodzie

Ten post edytował memory 25.08.2015, 17:47:20
Go to the top of the page
+Quote Post
Randallmaster
post
Post #6





Grupa: Zarejestrowani
Postów: 677
Pomógł: 11
Dołączył: 18.11.2009

Ostrzeżenie: (0%)
-----


memory czytam dokładnie?
i chodzi teraz o pierwszy punkt nie drugi...

Ten post edytował Randallmaster 25.08.2015, 18:19:17
Go to the top of the page
+Quote Post
Pyton_000
post
Post #7





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Ehh...
  1. resource('users', 'UsersController');
  2. get('users/{users?}', 'UserController@index');
Go to the top of the page
+Quote Post
Randallmaster
post
Post #8





Grupa: Zarejestrowani
Postów: 677
Pomógł: 11
Dołączył: 18.11.2009

Ostrzeżenie: (0%)
-----


Pyton_000 może ciągle błędnie czytam ale od początku:

  1. //routes.php
  2.  
  3. Route::resource('pages', 'PagesController');
  4. Route::get('pages/{page?}', 'PagesController@index');


routes list
Kod
GET|HEAD                       | pages                                                 | pages.index          | App\Http\Controllers\PagesController@index                 |            |
|        | POST                           | pages                                                 | pages.store          | App\Http\Controllers\PagesController@store                 |            |
|        | GET|HEAD                       | pages/create                                          | pages.create         | App\Http\Controllers\PagesController@create                |            |
|        | GET|HEAD                       | pages/{page?}                                         |                      | App\Http\Controllers\PagesController@index                 |            |
|        | GET|HEAD                       | pages/{pages}                                         | pages.show           | App\Http\Controllers\PagesController@show                  |            |
|        | PUT                            | pages/{pages}                                         | pages.update         | App\Http\Controllers\PagesController@update                |            |
|        | PATCH                          | pages/{pages}                                         |                      | App\Http\Controllers\PagesController@update                |            |
|        | DELETE                         | pages/{pages}                                         | pages.destroy        | App\Http\Controllers\PagesController@destroy               |            |
|        | GET|HEAD                       | pages/{pages}/edit                                    | pages.edit           | App\Http\Controllers\PagesController@edit                  |


Problem jest w tym, że jak teraz wejdę na stronę /pages/2 to korzysta mi z metody show a nie z index.
działa dopiero poprawnie jak routes zmienie na:

  1. //czyli wywale z routa show
  2.  
  3. Route::resource('pages', 'PagesController',['except' => ['show']]);
  4. Route::get('pages/{page?}', 'PagesController@index');


Sorry jeżeli się czepiam i marudzę, ale chcę dokładnie wiedzieć.
resful controllers nie uwzględnił używania paginacji na index? przecież to normalne że jeżeli dodać parametr do index będzie się mieszał z show chyba że źle myślę.

Ten post edytował Randallmaster 26.08.2015, 08:47:36
Go to the top of the page
+Quote Post
memory
post
Post #9





Grupa: Zarejestrowani
Postów: 616
Pomógł: 84
Dołączył: 29.11.2006
Skąd: bełchatów

Ostrzeżenie: (0%)
-----


Sam rozwiązałeś zagadkę :]. Kolejność ma znaczenie
  1. Route::resource('pages', 'PagesController']);
  2. Route::get('pages/{page?}', 'PagesController@index');
  3.  


Na początku masz trasy z resource dlatego pierwszy będzie show. Wystarczy zamienić miejscami i pierwsza trasa będzie index

  1. Route::get('pages/{page?}', 'PagesController@index');
  2. Route::resource('pages', 'PagesController']);


Nawiasem do paginacji nie jest ci potrzebny żaden parametr w metodzie index.

Ten post edytował memory 26.08.2015, 08:55:05
Go to the top of the page
+Quote Post
Pyton_000
post
Post #10





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


@memory ale On chce ładne url a nie tak jak obecnie paginacja ma ?page...
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 24.08.2025 - 00:21