Laravel 5 set route conditionally - laravel

Hi I want to get specific route only based on the condition. I currently tried Session but it doesn't work with routes. So anyone here might want to help if there's simpe way to this.
if(\Session::get('quiz_type') == 'quiz'){
Route::resource('quizzes.questions', 'QuestionsController');
}else{
Route::resource('surveys.questions', 'QuestionsController');
}
I want that certain route Quiz only if I passed and meet a certain condtion. Else I want to call on different route.

If I remember correctly Session start only after laravel has already parsed the routes file, this is why your code doesn't work as you expect.

Related

Is there any way that we can loop a function in a controller for multiple data validation?

I'm having a problem related to multiple data validation and want to know if there is any solution for loop condition of a function in a controller.If any please provide the solution.
I had tried using for loop but that dosent seem to work on Laravel Controllers
by default laravel supports multiple rules for validation, you have add | between the rules, like required|numeric|unique:posts. laravel will check the rules one after another.
Please add more details on your question and add your code.

how can i add #somelink to laravel route

I want to create a laravel route that links to a named anchor in a page. Could someone help with how to achieve that. Example: return Redirect::route('admin.articles.edit#somelink', $article->id);
In this case the route is in a controller which is redirecting back to the pages' comments section
I was looking for this and stumbled upon this older question.
The "possible duplicate" has an answer for the case where the link is created in a view (i.e. in html). It may not be immediately clear that this can also be used in returning a redirect route from a controller, as OP seemed to require (some years ago :).
At least in Laravel 5.5, it is possible to use this in the redirect()->to() function, e.g.:
return redirect()->to(
route('admin.articles.edit', [$article->id]) . "#somelink"
);
Based on this answer.

How to run single controller method from any route in Laravel 4

I really hope my question has been well thought out but here goes.
How do you implement something like
Route::get("/url1", "controller#method");
Route::get("hello/url1", "controller#method");
Route::get("hello/hi/url1", "controller#method");
in Laravel but using something like
Route::get("*/url1", "controller#method");
instead of declaring every route path?
I will explain why this problem has come up. You see the primary url is always changing because its being called from a js file via a location.href call. I could decide to use a primary url variable but its to be deployed via intranet to different servers in organizations and the primary url could change at any time meaning that localhost/project on one system might become localhost:7987/project on another thus breaking the url variable, now thats on one part. On the other hand there are js functions running continuously and when someone navigates to a deeper url, say from localhost/home to localhost/home/event a route call that should be independent of folder breaks
So yeah, I am wondering if theres a way to declare a global route that points to a controller and/or if this is possible in Laravel.
Thanks
Try this:
Route::get('{something}/url1', 'controller#method')->where('something', '*');
Not sure if that will work, but the idea is that you can use where to pass some Regexp to match selected value from route.

how route works in laravel with parameter

Maybe this is a very basic question but,
In laravel if i use this route:
Route::get('/{campo}','ItemController#show');
and then i try to use this route
Route::get('/mondo/','ItemController#mondo');
I get simply redirect to a ItemController#show with parameter 'mondo' but I can't reach ItemController#mondo because he take mondo like a parameter. How can I let laravel know when i want he take a variable from url and when i don't?
You just have to change the order:
Route::get('mondo','ItemController#mondo');
Route::get('{campo}','ItemController#show');
Laravel takes what comes first and your {campo} route was accepting every word you had in your URL.
Just flip your order.
first route to second and second one to first one

How to pass route values to controllers in Laravel 4?

I am struggling to understand something that I am sure one of you will be able to easily explain. I am somewhat new to MVC so please bear with me.
I have created a controller that handles all of the work involved with connecting to the Twitter API and processing the returned JSON into HTML.
Route::get('/about', 'TwitterController#getTweets');
I then use:
return View::make('templates.about', array('twitter_html' => $twitter_html ))
Within my controller to pass the generated HTML to my view and everything works well.
My issue is that I have multiple pages that I use to display a different Twitter user's tweets on each page. What I would like to do is pass my controller an array of values (twitter handles) which it would then use in the API call. What I do not want to have to do is have a different Controller for each user group. If I set $twitter_user_ids within my Controller I can use that array to pull the tweets, but I want to set the array and pass it into the Controller somehow. I would think there would be something like
Route::get('/about', 'TwitterController#getTweets('twitter_id')');
But that last doesn't work.
I believe that my issue is related to variable scope somehow, but I could be way off.
Am I going down the wrong track here? How do I pass my Controllers different sets of data to produce different results?
EDIT - More Info
Markus suggested using Route Parameters, but I'm not sure that will work with what I am going for. Here is my specific use case.
I have an about page that will pull my tweets from Twitters API and display them on the page.
I also have a "Tweets" page that will pull the most recent tweets from several developers accounts and display them.
In both cases I have $twitter_user_ids = array() with different values in the array.
The controller that I have built takes that array of usernames and accesses the API and generates HTML which is passed to my view.
Because I am working with an array (the second of which is a large array), I don't think that Route Parameters will work.
Thanks again for the help. I couldn't do it without you all!
First of all, here's a quick tip:
Instead of
return View::make('templates.about', array('twitter_html' => $twitter_html ))
...use
return View::make('templates.about', compact('twitter_html'))
This creates the $twitter_html automatically for you. Check it out in the PHP Manual.
 
Now to your problem:
You did the route part wrong. Try:
Route::get('/about/{twitter_id}', 'TwitterController#getTweets');
This passes the twitter_id param to your getTweets function.
Check out the Laravel Docs: http://laravel.com/docs/routing#route-parameters

Resources