fabby's avatar

General Route for all links

Hello, I try to set a general route for all links. For example, if i have an url like: www.example.com/account/create to go to controller account, action create www.example.com/product/5 to go to controller product, parameter = 5 www.example.com/cart/add/5 to go to controller cart, action add, id=5

can i set a generall route that will fulfill all this 3 conditions, or more? thanks!

0 likes
5 replies
bobbybouwmann's avatar

You can, but that is dangerous and error sensitive as well.. Not sure why you want to do this?

I would rather be explicit about the routes. Your routes.php file is one of the files which species the entries for your application. If you put this all in one route how do you know what kind of urls you have in your application?

fabby's avatar

i thinked of this sollution, because, before using laravel (started to use it for 2 weeks), i used kohana framework and there, i can set something like this: Route::set('default', '((/)(/)(/)(/))') ->defaults(array( 'controller' => 'welcome', 'action' => 'index', ));

and this means that i can have an url like: www.example.php/cart/add/param1/param2

where controller is cart, and the rests ar parameters that are send to public function action_index() And this is used for all the urls that has that structure (those parameters ar optional, so, no need to have it all)

The same i want in laravel, to set a structure, and to accept both get and post requests

Nick385's avatar

@fabby

To encapsulate your code use 3 grave accent's before and after the code to highlight it properly. so use 3x `

You can use a route that points to a controller the controller points to the view like:

Route::resource('projects', 'ProjectController');

is the same as

Route::delete('projects/{project}', 'ProjectController@destroy');
Route::get('projects/{project}/edit', 'ProjectController@edit');
Route::put('projects/{project}', 'ProjectController@update');
Route::post('projects/store', 'ProjectController@store');
Route::get('projects/create', 'ProjectController@create');
Route::get('projects/{project}', 'ProjectController@show');
Route::get('projects', 'ProjectController@index');

or you can specify what function of your controller you want to use

Route::resource('projects.tasks', 'ProjectTaskController', 
                ['only' => ['index', 'create']]);

maps to controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Task;

class ProjectTaskController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
    // project and task is a folder index is a .blade.php
        return view('project.task.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
    // project and task is a folder create is a .blade.php
        return view('project.task.create');
    }

without a controller

Route::get('/', function () {
    // pages a folder home is a .blade.php
    return view('pages.home');
});
fabby's avatar

@Nick385 i wanted something general, for all the pages, i don't want to add code in route for all the pages i create. Like i said upper

fabby's avatar

anoter example, if you can help me, so i have this 2 urls:

Route::get('/extension/getWebsites', array('uses' => 'Extension@getWebsites')); Route::get('/extension/checkUrl', array('uses' => 'Extension@checkUrl'));

is there a way to write only one rule, and to fulfill those 2 culs? 10x!

Please or to participate in this conversation.