I am working with Slim Framework. I would like to make dynamic routes so that my index.php files is not full of static routes.
Instead of having this that works :
$app->get('/mypage', function() use ($app) {
$app->render('mypage.php', compact('app'));
})->name('mypage');
I would like to have something like this (that does not work) :
$app->get('/:name', function($name) use ($app) {
$app->render('template.php', compact('app', 'name'));
})->name(:name);
thanks for you help !
->name(:name) assigns a name to the route. The name has to be a string. But you don't need this to create a dynamic route, you can just write
$app->get('/:name', function($name) use ($app) {
$app->render('template.php', compact('app', 'name'));
})
More on route names:
To create a named route: http://docs.slimframework.com/#Route-Names
To use a named route: http://docs.slimframework.com/#Route-Helpers
(the URL For paragraph)
Update: The above is an answer for the V2 version of the Slim framework. More on naming routes in Slim V3 here: https://www.slimframework.com/docs/objects/router.html#route-names
Related
The routes/web.php have common application routes, Like showing the views, Getting the data from the form to a controller method.
The routes/api.php will have routes for getting records of the tables/entities in JSON. Updating, Deleting etc via api routes ?
Question 1 : How will i use my routes/api.php routes to get, delete, Update data/Records ?
Question 2 : Android app will be able to use my api ? If yes, How ?
Question 3 : The API should have a controller ApiController ?
Let the kid know the stuff !
Thanks
1:
Put this into your RouteServiceProvider file:
$router->group(['namespace' => $this->webNamespace], function ($router) {
require app_path('routes/web.php');
});
$router->group(['namespace' => $this->apiNamespace], function ($router) {
require app_path('routes/api.php');
});
1.1 in routes/web.php file (alternative):
require_once "../../routes/api.php";
2:
yes. use your routes as you would use them in any other frontend application. For example: localhost/api/myAndroidRoute
3:
doesn't really matter. you can use one controller for all the routes, or one controller for each route. Whichever option you like. If you have a lot of code, separate it into different controllers for better readibility.
For example:
Route::get('api/sampledata', 'ApiController#getSampleData'); // controller#function
Route::get('api/otherdata', 'SomeOtherController#getOtherData');
or
Route::get('api/sampledata', 'AllmightyController#getSampleData');
Route::get('web/otherdata', 'AllmightyController#getOtherData');
I am absolutely novice with Laravel and I am currently learning the routes feature of the Framework.
I want to know if it is possible to do like we can do with ASP.NET that is to redirect to a Controller written in the URL.
Actually, to do something like:
<?php
Route::get('/{CustomController}/{Action}', function ($controller, $action) {
return controller($controller, $action);
});
Where CustomController is the controller which will run the request to the view Action
With ASP.NET Core, it is something like:
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});
Yes, with routes in laravel the format is this:
Route::get('/custom-controller', 'CustomController#index');
If you are using the standard method names in your controller such as index(), store() etc
you can use resource instead and it will automatically build up all your routes:
Route::resource('/my-route', 'CustomController');
If you then do php artisan route:list you will see laravel has automatically added all the standard routes for your controller.
I'm using the extension laravel-menu in my Laravel application.
This application contains multiple projects with multiple locations attached to each project.
Now I want to define a sidemenu where I can among other manage the locations.
The url of a project is
project/1
The url of the locations page of a project is
project/1/locations
How to setup this side menu in routes.php?
My routes.php code:
Route::resource('project', 'ProjectsController'));
Route::resource('project.locations', 'LocationsController');
Menu::make('sidemenu-project', function($menu) {
$menu->add('Locaties', array('route' => 'project.locations.index','{project?}'))->data('id',1); // this is not working
});
This is outputting the url /project/%7Bproject%7D/locations
Go to your terminal (Command Prompt) and run following command:
> php artisan routes
Then you'll see all the declared routes with their URL and corresponding route name and method name.
I'm very new to Laravel but the Routes page of documentation mentions you create a controller with parameters like this:
Route::get('user/{id}', function($id) { ... });
could you therefore define your route as
Route::get('project/{id}/locations', function($id) { ... });
I think you have this issue due to misconfiguring the routes. To achieve the route structure that you want, you should put your project/1/locations route definition above the first one. Consider your routes.php to be:
Route::resource('project/{project}/locations', ['as'=>'project.locations', 'uses'=> 'LocationsController']);
Route::resource('project', 'ProjectsController'));
I have the following association between a route and a callback in my application:
$api->slim->post('/:accountId/Phone-Numbers/', function($accountId) use ($api) {
$api->createPhoneNumber($accountId);
});
What I want to avoid is having the route myhost/a7b81cf/phone-numbers/ return a 404 response because Slim understands the route myhost/a7b81cf/Phone-Numbers/ as being different, due to the usage of uppercase letters.
How can I avoid setting up two separate routes that trigger the same callback function?
This is an old question, but I wanted to provide a clear answer for this problem.
There is a 'routes.case_sensitive' configuration that allows you to do that. I don't know exactly why this is not in the docs (http://docs.slimframework.com/#Application-Settings), but if you look at the framework's source code (specifically, in getDefaultSettings() at Slim.php) you can see that is there.
I just tested it and it works fine.
Summarizing, the solution is to apply the 'routes.case_sensitive' configuration like this:
$configurations = [
// ... other settings ...
'routes.case_sensitive' => false
];
$app->config($configurations);
You could try with route validation/conditions:
$api->slim->post('/:accountId/:phoneNumbers/', function ($accountId) {
$api->createPhoneNumber($accountId);
})->conditions(array('phoneNumbers' => '(p|P)hone\-(n|N)umbers'));
Or if you want more global change you could override Route->matches method in Route class to be case insensitive but it will affect whole application.
You can mimic case-insensitive routing in Slim by registering a hook and modifying the incoming routes to match the case of your defined routes.
In the example, all of your defined routes should be lowercase and strtolower is called on all incoming paths:
$app->hook('slim.before.router', function () use ($app) {
$app->environment['PATH_INFO'] = strtolower($app->environment['PATH_INFO']);
});
How can I create admin specific routes in Laravel 4 (Restfull Controllers):
/admin/users (get - /admin/users/index)
/admin/users/create (get)
/admin/users/store (post)
I want to know:
What Files and where I need create theam
How I need create the route
In Laravel 4 you can now use prefix:
Route::group(['prefix' => 'admin'], function() {
Route::get('/', 'AdminController#home');
Route::get('posts', 'AdminController#showPosts');
Route::get('another', function() {
return 'Another routing';
});
Route::get('foo', function() {
return Response::make('BARRRRR', 200);
});
Route::get('bazz', function() {
return View::make('bazztemplate');
});
});
For your subfolders, as I answer here "route-to-controller-in-subfolder-not-working-in-laravel-4", seems to have no "friendly" solution in this laravel 4 beta.
#Aran, if you make it working fine, please add an code sample of your controller, route, and composer.json files :
Route::resource('admin/users', 'admin.Users');
or
Route::resource('admin', 'admin.Users');
thanks
Really useful tool that you can use is the artisan CLI.
Using this you'll be able to generate the needed function file with all the required routes for it to become RESTful.
php artisan controller:make users
Would generate the function file for you. Then in your routes.php file you can simply add
Route::resource('users', 'Users');
This'll setup all the necessary routes.
For more information on this you should read the documentation at.
http://four.laravel.com/docs/routing#resource-controllers
http://four.laravel.com/docs/artisan
Edit:
To make this admin specific, simple alter the code like follows and move the controller to a admin folder inside the controllers folder.
Route::resource('admin/users', 'admin.Users');
The first paramater is the route, the second is the controller filename/folder.
In Laravel if you placed a controller inside a folder, to specific it in a route or URL you'd use the a dot for folders.
You can then expand on this and add Authentication using Route Filters and specifically the code found "Pattern Based Filters" found on the page below.
http://four.laravel.com/docs/routing#route-filters
Laravel 4 - Add Admin Controller Easily
This was driving me insane for ages, but i worked it out.
routes.php
Route::resource('admin', 'Admin_HomeController#showIndex');
/controllers/Admin/HomeController.php
Notice the folder name Admin must be captital 'A'
<?php
class Admin_HomeController extends Controller {
public function showIndex() {
return 'Yes it works!';
}
}
Alternatively you can use the group method
Route::group(array('prefix' => 'admin'), function() {
Route::get('/', 'Admin_HomeController#showIndex');
});
Thanks
Daniel