Laravel link to Controller

12,703

Solution 1

Actualy Laravel will create following routes

Route(URL)                                      | Name
------------------------------------------------------------------
GET profile                                     | profile.index
GET profile/create                              | profile.create
POST profile                                    | profile.store
GET profile/{profile}                           | profile.show
GET profile/{profile}/edit                      | profile.edit
PUT profile/{profile}                           | profile.update
PATCH profile/{profile}                         |
DELETE profile/{profile}                        | profile.destroy

For this

Route::resource('profile' , 'ProfileController' , array('as'=>'profile' , 'before'=>'csrf'));

If you run php artisan routes from your terminal/command prompt then you'll get all the route's list with name and url.

Solution 2

Probably you'll have to:

<li><a href="{{ URL::route('profile.index') }}"> Profile Managment </a></li>

To be sure, execute

php artisan routes

Laravel will show the list of routes and names you have to use.

Also, you have access to more info in the docs: http://laravel.com/docs/controllers#resource-controllers

Share:
12,703
DolDurma
Author by

DolDurma

Updated on June 04, 2022

Comments

  • DolDurma
    DolDurma almost 2 years

    i have this menu item such as :

    <li><a href="{{ URL::route('profile') }}"> Profile Managment </a></li>
    

    and i want to route that to ProfileController. this below my route do not work.

    Route::resource('profile' , 'ProfileController' , array('as'=>'profile' , 'before'=>'csrf'));
    

    i want to route that if user can login and can see profile page and that nust be send all Request to ProfileController.

    Error Result:

    Error in exception handler: Route [profile] not defined. (View: /var/www/laravel/app/views/back_end/menu.blade.php) (View: /var/www/laravel/app/views/back_end/menu.blade.php) (View: /var/www/laravel/app/views/back_end/menu.blade.php) in /var/www/laravel/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:207
    
  • DolDurma
    DolDurma about 10 years
    my profile view is under view/back_end/layouts and URL::route('profile.index') for link to ProfileController with http://localhost/laravel/public/admin/profile link. i get this error:Route [profile.index] not defined.
  • The Alpha
    The Alpha about 10 years
    Run php artisan routes to be sure you have a route as profile.index and also, run composer dump-autoload to update the autoload class.
  • DolDurma
    DolDurma about 10 years
    thanks. my form action is ProfileController@profile and in ProfileController i have this action:public function profile(){ print_r(Input::all());}. i get this error now. Route [ProfileController@profile] not defined.
  • The Alpha
    The Alpha about 10 years
    To submit the form to store method you should use {{ Form::open(array('route' => 'profile.create', 'method' => 'post')) }} or you may use action="{{ route('profile.store') }}" method="POST".
  • The Alpha
    The Alpha about 10 years
    If you want to submit the form to the profile method then it needs a route.
  • DolDurma
    DolDurma about 10 years
    thanks after change route to admin.profile.create my url changed to http://localhost/laravel/public/admin/profile/{profile} and i get this error: NotFoundHttpException. My form is: http://paste.debian.net/79337/. My Route: http://paste.debian.net/79339/
  • The Alpha
    The Alpha about 10 years
    What you are trying to do, where you want to submit the form, did you use Form class of Laravel, if not then use it, check the documentation.
  • DolDurma
    DolDurma about 10 years
    did you try to see my links? i'm using laravel opening form with Blade. http://paste.debian.net/79337/
  • The Alpha
    The Alpha about 10 years
    Do you have a route named as admin.profile.update ?
  • DolDurma
    DolDurma about 10 years
    my problem is that. why i must be have route? i want to send form to controller directly.i must define route for that?
  • The Alpha
    The Alpha about 10 years
    This is the easiest way, you can use url like array('url' => 'profile/update/1', 'method' => 'post')), this route needs an id of the record you want to update.
  • The Alpha
    The Alpha about 10 years
    With route, array('route' => array('profile.update', $someModel->id), 'method' => 'post')).
  • DolDurma
    DolDurma about 10 years
    i'm sorry how to fix this route? can you help me? Route::post('/admin/profile/{id}',array('as'=>'admin.profile‌​.update',function($i‌​d){}));
  • The Alpha
    The Alpha about 10 years
    Sorry! I don't get it, you suppose to use resource controller's route according to your question.
  • DolDurma
    DolDurma about 10 years
    yes. i'm using Route::resource and i can not resolve this problem.
  • The Alpha
    The Alpha about 10 years
    Then you don't need to use Route::post('/admin/profile/{id}',array('as'=>'admin.profile‌​.update',function($‌‌​​id){}));, remove it and use the update function inside ProfileController for updating and use {{ Form::open(array('route' => array('profile.update', $someModel->id), 'method' => 'post')) }}.