I want my user to access its profile edit page by URL: /profile/slug/edit, where slug means $user->slug.
My web.php contans:
Route::group(['middleware' => 'auth'], function () {
Route::get('/profile/{slug}', [
'uses' => 'ProfilesController#index',
'as' => 'profile'
]);
Route::get('/profile/{slug}/edit', [
'uses' => 'ProfilesController#edit',
'as' => 'profile.edit'
]);
How to call ProfilesController#edit from view, how to pass parameters correctly? Tried:
<a href="{{route('profile', ['slug'=> Auth::user()->slug],'edit')}}">
Edit your profile</a>
Here is how I would do it..
Route::group(['middleware' => 'auth'], function () {
Route::get('/profile/{slug}', 'ProfilesController#index')->name('profile');
Route::get('/profile/{slug}/edit', 'ProfilesController#edit')->name('profile.edit');
});
And then in your view, you can use..
Edit your profile
As you can see, first we have to give the route() the route name we are interested in, in your case it's profile.edit that is the target route, and we know from our routes file that it's missing the slug value, so we provide it the slug value as the second argument (if there are more missing values, the second argument should be an array).
It takes some practice and time but try different ways to see what makes your code more readable. The number of lines doesn't matter that much to the computer, write the code so you can easily read and understand it if you want to change something a year or two from now.
You can use following codeline
Edit your profile
Your routes definitions seems to be fine.
Plus if you want to add some get params, you can add directly in the array passed as the second argument
Edit your profile
Hope this helps. :)
Related
I have a route with parameter
Route::get('forum/{ques}', "ForumQuestionsController#show");
Now I want a route something like
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
well when I hit localhost:800/forum/add I get routed to ForumQuestionsController#show instead of ForumQuestionsController#add
Well I know I can handle this in show method of ForumQuestionsController and return a different view based on the paramter. But I want it in this way.
First give this one
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
Then the following
Route::get('forum/{ques}', "ForumQuestionsController#show");
Another Method (using Regular Expression Constraints)
Route::pattern('ques', '[0-9]+');
Route::get('forum/{ques}', "ForumQuestionsController#show");
If ques is a number it will automatically go to the show method, otherwise add method
You can adjust the order of routes to solve the problem.
Place add before show , and then laravel will use the first match as route .
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
Route::get('forum/{ques}', "ForumQuestionsController#show");
I think your {ques} parameter do not get properly. You can try this:
Route::get('forum/show/{ques}', "ForumQuestionsController#show");
Route::get('forum/add', ['middleware' => 'auth:student', 'uses' => "ForumQuestionsController#add"]);
If you use any parameters in show method add parameters:
public function show($ques){
}
So building a few pages on the same template and loading the content via AJAX. Most of the content are forms. Views are defined by step number (1,2,3,4,5....32)
Here is how I built my route:
Route::get('onboarding/', [
'as' => 'get-onboarding-start',
'uses' => 'OnboardingController#getStart'
]);
Route::get('onboarding/{i}', [
'as' => 'get-onboarding-step',
'uses' => 'OnboardingController#getNextStep'
]);
Route::post('onboarding/{i}', [
'as' => 'post-onboarding-step',
'uses' => 'OnboardingController#postStepForm'
]);
Now one method in the controller cannot handle all the work. Meaning I will need to redirect to another method based on the $i (step number).
I am afraid that it is not simple to read if I put a big blog of switch case $i = 1,2,3...
At the same time I don't want to write 32 different routes.
What would you propose?
Hard code all the routes meaning: 'onboarding/username' then
'onboarding/email' etc... etc... The good point is that it is super
simple to read in the views and you know exactly what the next step
is... no need to check what the number corresponds to.
Catch all as coded now and redirect to different methods in the controller
Something better, super easy to read and with little lines of
code... which is .... ??
If these steps are going to remain as they are without many changes in the future, I'd go for the first option (having 32 get & 32 post routes). This will keep your application simple, if you'd want to apply parameters or middleware to them you can use route groups. Below I've posted a small code example from the laravel documentation
Route::group(['middleware' => 'auth'], function () {
Route::get('/', function () {
// Uses Auth Middleware
});
Route::get('user/profile', function () {
// Uses Auth Middleware
});
});
My laravel application has a model - Video. It is the main model so the route was named videos. But after the development I discovered that there is a folder on the production server named videos
So now rewriting the url to include index.php in .htaccess does not work.
I cannot change the name of videos folder which is already present.
I cannot change the db table name either. I don't want to do that, its too much work.
Is there a way to change the route name to something else like lvideos or vvideos?
I tried changing it in routes but it seems there are other places where I have to change it. It throws me an error in the controller.
Can anyone suggest a solution for this?
I don't want to give the link with index.php to the users
Thank you.
You will have to change the route anywhere it is referenced.
In the future if you think a route might change, you could use named routes and then reference the route name anywhere you need to use it.
For example:
Route::group(['prefix' => 'videos'], function() {
Route::get('/', [
'uses' => 'VideosController#index',
'as' => 'videos.index',
]);
Route::get('{id}', [
'uses' => 'VideosController#show',
'as' => 'videos.show',
]);
});
Then everywhere you use these routes you use the name, for example in a view:
Videos
The link will still work even if you change the route to Route::group(['prefix => 'iVideos']); Even though the route changed, the name did not.
what I'm trying to do is set it up so that the user can go to "/project/index" ("/" being the route ofc) but I'm not quite sure how to do it in laravel?
What I currently have:
Routing:
Route::get('project.index', array('as' => 'project/index', 'uses' => 'ProjectController#indexPage'));
Also in routing:
View::addLocation('project'); //Project View
View::addNamespace('project', 'project');
In my Project Controller:
public function indexPage()
{
return View::make('index', array('pageTitle' => 'Project Index'));
}
Any ideas? Thanks in advance.
PS: It's Laravel 4
You have your routing a little wrong. Try out the following
Route::get('project/index', ['as' => 'project.index', 'uses' => 'ProjectController#index']);
So the first parameter into the Route::get() function should be the URL the user is visiting for example http://example.com/project/index. The as key in the array provided is the name you're giving to the route.
By giving the route a name you can use this throughout your application, rather than using the url the user is visiting. For example you might want to generate a link to your route
Link
This will generate a link to http://example.com/project/index. This makes it convenient in the future should you wish to change your URLs without changing lots of links throughout your view files.
Route::get('foobar/index', ['as' => 'project.index', 'uses' => 'ProjectController#index']);
The URL generated through route('project/index') would now be http://example.com/foobar/index
Checkout the routing documentation for further information http://laravel.com/docs/4.2/routing
I'm seeing an issue with Laravel 4 when I have two routes pointing to the same action, one within a group and one just "loose" in the routes.php file.
<?php
// Routes.php
Route::group(array('domain' => '{subdomain}.domain.com'), function()
{
Route::get('profile/{id}/{name}', 'ProfileController#index');
});
Route::get('profile/{id}/{name}', 'ProfileController#index');
// Template.blade.php
Jim Smith
The template links to: currentsubdomain.domain.com/profile/%7Bid%7D/%7Bname%7D instead of the expected behaviour of swapping the ID and name for 123 and JimSmith respectively.
If I comment out, the first route (the one within the group), the code works as expected. Why does adding this additional route break the URL generation? Is there a way to work around this? Am I missing something obvious?
P.s. For those wondering why I need this route in two places it's so I can optionally generate the url with the subdomain using URL::action('ProfileController#index' array('subdomain' => 'james', 'id' => 123, 'name' => 'JimSmith');
The problem is that you don't have names/aliases for the routes so it's defaulting to the first one it comes across.
Consider this an alternate route structure:
Route::group(array('domain' => '{subdomain}.domain.com'), function() {
Route::get('profile/{id}/{name}', [
'as' => 'tenant.profile.index',
'uses' => 'ProfileController#index'
]);
});
Route::get('profile/{id}/{name}', [
'as' => 'profile.index',
'uses' => 'ProfileController#index'
]);
Now that you have these routes named, you can do:
{{ URL::route('profile.index', [123, 'jSmith']) }}
Or alternatively:
{{ URL::route('tenant.profile.index', ['subdomain', 123, 'jSmith']) }}
As just an added extra, you could only have this route defined once, then in all the controller methods you'd have something like:
public function index($subdomain = null, $id, $name) { }
Then you can just simply pass www through as the subdomain and have some code somewhere that discounts the www.domain.com domain from certain actions.
Multi-tenancy (if that is indeed what you're after) isn't easy and straight forward but there are some methods used to tackle certain parts. I'm actually planning on writing a tutorial regarding it, but for now I hope this helps somewhat.