I need to match some different routes to aim the same page.
E.g.: / , /inicio, /home -> must show the landing page
$app->get('/', function () use ($app) {
return $app['twig']->render(
'home.html.twig',
array(
'page' => 'home'
)
);
})
->bind('home');
And I'd like to avoid the same for each route. I'd like to do something like
$app->match('/|/home|/inicio, function() use ($app) {} );
Maybe you're looking for this:
$app->get("/{slug}", function ($slug) use ($app) {
// your regular action logic
})->assert("slug", '^(home|inicio)$');
Here's the link to the docs on route requirements.
Related
I need to redirect from a controller to another.
$app->get('/test', function(Request $request) use ($app) {
return $app->redirect($app["url_generator"]->generate("success", [
"myArg" => $myArg
]));
});
$app->get('/test/success', function(Request $request, $myArg) use ($app) {
return $app['twig']->render('confirmation.twig', [
'myArg' => $myArg,
]);
})->bind('success');
But it seems to doesn't be the good way to do it... I just want to redirect to the route "/test/success" and passing the $myArg variable to my confirmation.twig template.
Thanks for help.
By putting $myArg in the arguments of your function, Silex expects it to be a parameter from your URL, however your route definition isn't dynamic so it can't work.
If you want to get the parameter from the arguments of your function you have to modify your route definition to something like /test/success/{myArg}.
How to get value of one route into another
Route::get('/first_url', function () {
return "Hello this is test";
});
I Tried something like this but not worked.
Route::get('/second_url', function () {
$other_view = Redirect::to('first_url');
});
I want to get returned value from first_url to variable $other_view in second_url to process and manipulate returned value.
Using Redirect is changing url. Which I dont want to use.
Any Idea ??? Or Am I trying wrong thing to do.
If you just want to return first_url, do this:
Route::get('/first_url', ['as' => 'firstRoute', function () {
return "Hello this is test";
}]);
Route::get('/second_url', function () {
return redirect()->route('firstRoute');
});
Learn more about redirects to routes here.
Update:
If you want to pass variable, you can use form or just create a link with parameters. You can try something like this {{ route('second_url', ['param' => 1]) }}
Then your second route will look like this:
Route::get('/second_url/{param}', ['uses' => 'MyController#myMethod', 'param' => 'param']);
And myMethod method in MyController:
public function myMethod($param){
echo $param;
...
I don't know why you would like to do this, but you can get the rendered contents of the route by executing a simple HTTP request to your route and reading the contents:
Route::get('/second_url', function () {
$other_view = file_get_contents(URL::to('first_url'));
return $other_view; // Outputs whatever 'first_url' renders
});
You need to send HTTP request and then process the response. You can use file_get_contents as #tommy has suggested or you can use HTTP library like Guzzle:
$client = new GuzzleHttp\Client();
$res = $client->get(route('firstRoute'));
in this case u should use a named route.
https://laravel.com/docs/5.1/routing#named-routes
somthing like this:
Route::get('/first_url', ['as' => 'nameOfRoute', function () {
return "Hello this is test";
}]);
Route::get('/second_url', function () {
redirect()->route('nameOfRoute');
});
You can not pass the variable value to another route directly. http is stateless protocol. if you want to preserve the value of variable to another route you can do that by 3 methods query string, sessions and cookies only. Your can pass parameter to to specific route like this
Route::get('/second_url/{param}', ['uses' => 'MyController#myMethod',
'param' => 'param']);
The idea behind achieving what you want is naming the function of your first route and calling it within both the first route and your second route. Your function will simply return the view to the first route, and retrieve the rendered html for your second.
function MyFirstRouteFunction() {
// Do whatever your do in your first route
// I assume your function return here an instance of Laravel's View
}
Route::get('/first_url', MyFirstRouteFunction);
Route::get('/second_url', function () {
$contentsOfFirstRoute = MyFirstRouteFunction()->render();
// Make use of rendered HTML
});
There is no need to make one extra HTTP request.
You should use Guzzle or curl to achive this:
Route::get('/second_url', function () {
//:::::Guzzle example:::::
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'http://...second_url...', []);
//:::::curl example:::::
$ch = curl_init();
//define options
$optArray = array(
CURLOPT_URL => 'http://...second_url...',
CURLOPT_RETURNTRANSFER => true
);
//apply those options
curl_setopt_array($ch, $optArray);
//execute request and get response
$res = curl_exec($ch);
});
Note that using Guzzle may need you to install required libraries.
If you put your first_route closure into a controller action you could try to instantiate that controller and call the method directly.
This is considered as bad practice.
routes.php
Route::get('/first_url', 'TestController#getFirstUrl');
App/Http/Controllers/TestController.php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class TestController extends Controller
{
public function getFirstUrl()
{
return view('my-view');
}
}
routes.php
Route::get('/second_url', function () {
$controller = new \App\Http\Controllers\TestController();
$contentsOfFirstRoute = $controller->getFirstRoute();
// Make use of rendered HTML
});
How do I use slim framework route get all .. but not include except string get /login
$app->get('/.*?', function () use ($uri, $app) {
$app->redirect($uri['public'].'/login');
});
$app->get('/login', function () use ($uri, $app) {
echo 'login view';
});
...
$app->post('/login', function () use ($uri, $app) {
$user_controller = new controller\user_controller();
$user_controller_login = $user_controller->login($uri, $app);
});
Slim routes are processed in order, so if you define the /login route before the catch-all, it will work in that order:
$app->get('/login', function() use($app) { ... });
$app->post('/login', ...);
$app->get('/.*', function() use($app) { $app->redirect('/login'); });
Although, I don't usually see 'catch all' style routes. Usually, you'd use URL rewriting to pass to static files not served by routes, and if you're doing this to ensure the user has logged-in to each page, you are better off using Slim Middleware to handle that.
For instance, if you had an authenticate piece of middleware, it could check on each of your routes for a login session/cookie/whatever, and if not found redirect to the login page (also passing the current url so they could be redirected back after login).
$authenticate = function() {
$app = \Slim\Slim::getInstance();
return function() use($app) {
// check for auth here somehow
if (!isset($_SESSION['user'])) {
$app->redirect('/login');
}
};
}
// Use middleware on your defined routes
$app->get('/stuff', $authenticate, function() use($app) { ... });
...
I have one installation of Laravel on which I wish to run 3 sites (addon domains). I am using Laravel's route grouping method to grab each domain. However, I need to be able to know which domain I am working with inside of each group. What is the best way to do this? I have the following code:
Route::group(array('domain' => 'domainone.com'), function($domain = 'domainone')
{
Route::get('/', function($domain) {
//
});
});
^ Which doesn't work.
The notes suggest using wildcards in the URL, eg.
Route::group(array('domain' => '{domain}.com'), function()
{
Route::get('/', function($domain) {
//
});
});
However, I would prefer a different method, as I can't really use this during development on my local server. Is there any way that is more like my first method, in which I can just manually declare a key for each domain?
EDIT: I also then need to pass that domain variable to a controller, which I am also struggling to work out how to do?
Thanks.
EDIT 2
My problem is that I am not using subdomains, I am using domains; I have 3 separate domains for 3 sister sites that are running on the same installation of Laravel. So I have 3 route groups, one for each domain. Moreover, I don't want to request the domain variable using {domain}.com each time, I want to tell Laravel the domain in each case, then pass that domain as a variable to the appropriate controller within each group. Here is my example code:
$domain1 = 'domain1.com';
$domain2 = 'domain2.com';
$domain3 = 'domain3.com';
Route::group(array('domain' => $domain1), function(){
Route::get('/', 'PrimaryController#somefunction1'); // <-- I want to access $domain1 in my controller
});
Route::group(array('domain' => $domain2), function(){
Route::get('/', 'PrimaryController#somefunction2'); // <-- ...and $domain2
});
Route::group(array('domain' => $domain3), function(){
Route::get('/', 'PrimaryController#somefunction3'); // <-- ...and $domain3
});
This is an option for your first method:
$domain = 'domainone';
Route::group(array('domain' => $domain.'.com'), function() use ($domain)
{
Route::get('/', function() use ($domain) {
echo "$domain";
});
});
You can pass watever you like to your controllers, via groups too, you just need to add one more level.
$subdomain = 'atlanta';
$domain = 'domainone';
Route::group(array('domain' => "$subdomain.$domain.com"), function()
{
Route::group(array('domain' => '{subdomain}.{domain}.com'), function()
{
Route::get('testdomain', function($subdomain, $domain) {
dd("closure: subdomain: $subdomain - domain: $domain");
});
Route::get('testdomaincontroller', 'FooController#index');
});
});
By doing this you have to understand that your first two variables passed to your controller action will always be $subdomain and $subdomain. Here's a controller to show it, which you can use to test those routes too:
class FooController extends Controller {
public function index($subdomain, $domain)
{
dd("controller: subdomain: $subdomain - domain: $domain");
}
}
You'll have two different routes with this:
http://yourdomain.dev/testdomain
http://yourdomain.dev/testdomaincontroller
I accomplish this with the following two steps:
First, using the Laravel documentation example, I pull the subdomain down and assign it to {account}.
Route::group(array('domain' => '{account}.myapp.com'), function()
From here, any controller you assign to a route within this group will have the ability to inject this {account} data in to its functions.
Note: you can't access it in the constructor. Each function in the Controller that needs to use this data will need a parameter created for it. Like this:
/**
* The $subdomain variable below will capture the data stored in
* {account} in your route group. Note that you can name this variable
* anything you'd like.
*/
public function showCreateBankAccount($subdomain) {
echo "This is your subdomain: " . $subdomain;
}
I have created the Authentication, and its working perfectly. But there is some problem in checking the inner pages. For example,
Route::get('/', array('before' => 'auth' , 'do'=> function(){
return View::make('home.index');
}));
The index page is only visible for logged in users. But whenever I have go to the inner pages, for example example.com/products. The products page can be visible without log in.
Here is my solution.
/**
* Groups of routes that needs authentication to access.
*/
Route::group(array('before' => 'auth'), function()
{
Route::get('user/logout', array(
'uses' => 'UserController#doLogout',
));
Route::get('/', function() {
return Redirect::to('dashboard');
});
Route::get('dashboard', array(
'uses' => 'DashboardController#showIndex',
));
// More Routes
});
// Here Routes that don't need Auth.
There are several ways of applying filters for many routes.
Putting rotues into Route::group() or if you using controllers add the filter there, add it in the Base_Controller so it will be applied to all. You can also use filter patterns and use a regex which applies the filter to all except a few you don't want to.
Documentation
Route filters: http://laravel.com/docs/routing#route-filters
Example to the pattern filter, as the others are basicly in the docs. This one could be the fastest but also the most problematic because of the problematic way of registering a regex in this function (the * is actually converted into (.*)).
Route::filter('pattern: ^(?!login)*', 'auth');
This will apply auth to any route except example.com/login.
Route::group(['middleware' => ['auth']], function()
{
Route::get('list', 'EventsController#index');
});
Read more on the documentation page:
https://laravel.com/docs/5.2/routing#route-groups
There may be a better way but I take a whitelist approach. Everything is blocked from public except for what the pages I put in this array.
// config/application.php
return array(
'safe' => array(
'/',
'card/form_confirm',
'record/form_create',
'card/form_viewer',
'user/login',
'user/quick_login',
'user/register',
'info/how_it_works',
'info/pricing',
'info/faq',
'info/our_story',
'invite/accept',
'user/terms',
'user/privacy',
'email/send_email_queue',
'user/manual_login',
'checkin/',
'checkin/activate',
'system/list',
),
// routes.php
Route::filter('before', function()
{
// Maintenance mode
if(0) return Response::error( '503' );
/*
Secures parts of the application
from public viewing.
*/
$location = URI::segment(1) . '/' . URI::segment(2);
if(Auth::guest() && !in_array( $location, Config::get('application.safe')))
return Redirect::to( 'user/login' );
});
this code working fine with me
Auth::routes();
Route::group(['middleware' => 'auth'], function () {
// Authentication Routes...
Route::get('/', 'HomeController#index')->name('home');
});
The same problem can be solved using a BaseController to extends all Controller have must logged user.
Example:
class SomeController extends BaseController
{
public function index() { return view('some.index');}
}
just add a __construct() method to BaseController
class BaseController extends Controller
{
protected $redirectTo = '/myIndex'; // Redirect after successfull login
public function __construct()
{
$this->middleware('auth'); // force all controllers extending this to pass auth
}
}
More info here
Just check if user is logged in in your views.
Or restrict all controller (if you use it)
Or check Route Groups, and give a filter to whole group of routes: http://laravel.com/docs/routing#groups
Route::filter('pattern: /*', array('name' => 'auth', function()
{
return View::make('home.index');
}));
It worked for me . take a look at it.
Route::when('*', 'auth.basic');
Route::get('api/getactorinfo/{actorname}', array('uses' =>'ActorController#getActorInfo'));
Route::get('api/getmovieinfo/{moviename}', array('uses' =>'MovieController#getMovieInfo'));
Route::put('api/addactor/{actorname}', array('uses' =>'ActorController#putActor'));
Route::put('api/addmovie/{moviename}/{movieyear}', array('uses' =>'MovieController#putMovie'));
Route::delete('api/deleteactor/{id}', array('uses' =>'ActorController#deleteActor'));
Route::delete('api/deletemovie/{id}', array('uses' =>'MovieController#deleteMovie'));