Asked  7 Months ago    Answers:  5   Viewed   35 times

Using Laravel 4.2, is it possible to assign a name to a resource controller route? My route is defined as follows:

Route::resource('faq', 'ProductFaqController');

I tried adding a name option to the route like this:

Route::resource('faq', 'ProductFaqController', array("as"=>"faq"));

However, when I hit the /faq route and place {{ Route::currentRouteName() }} in my view, it yields faq.faq.index instead of just faq.

 Answers

72

When you use a resource controller route, it automatically generates names for each individual route that it creates. Route::resource() is basically a helper method that then generates individual routes for you, rather than you needing to define each route manually.

You can view the route names generated by typing php artisan routes in Laravel 4 or php artisan route:list in Laravel 5 into your terminal/console. You can also see the types of route names generated on the resource controller docs page (Laravel 4.x | Laravel 5.x).

There are two ways you can modify the route names generated by a resource controller:

  1. Supply a names array as part of the third parameter $options array, with each key being the resource controller method (index, store, edit, etc.), and the value being the name you want to give the route.

    Route::resource('faq', 'ProductFaqController', [
        'names' => [
            'index' => 'faq',
            'store' => 'faq.new',
            // etc...
        ]
    ]);
    
  2. Specify the as option to define a prefix for every route name.

    Route::resource('faq', 'ProductFaqController', [
        'as' => 'prefix'
    ]);
    

    This will give you routes such as prefix.faq.index, prefix.faq.store, etc.

Wednesday, March 31, 2021
 
PeanutsMcgee
answered 7 Months ago
80

The problem is in the usage of the PSR-4 Since Laravel default is PSR-0 it assumes that the resources (views etc) of a package will be 2 levels up from where the package service provider is. Ex:

src
??? config
??? lang
??? migrations
??? Ghunti
?   ??? Subby
?       ??? SubbyServiceProvider.php
??? routes.php
??? views
    ??? user
        ??? login.blade.php

With PSR-4 the package service provider and the views will be at the same level (and the error "No hint path defined for" will show up:

src
??? config
??? lang
??? migrations
??? SubbyServiceProvider.php
??? routes.php
??? views
    ??? user
        ??? login.blade.php

To fix this, on the package service provider boot() method, instead of:

public function boot()
{
    $this->package('ghunti/subby');
}

we need to specify the resources path (the 3rd parameter)

public function boot()
{
    //For PSR-4 compatibility we need to specify the correct path (3rd parameter)
    $this->package('ghunti/subby', null, __DIR__);
}
Saturday, May 29, 2021
 
barden
answered 5 Months ago
14

You can pass your route parameters as the second argument to route() helper:

<a href="{!! route('organisations.index', ['menu' => 'p11-c3']) !!}">

Make sure you're using correct names. Your routing defines subscription.create route while your template refers to organisations.index route.

Saturday, May 29, 2021
 
Blur
answered 5 Months ago
36

You can do this

    Route::get('/profile', 'HomeController@profile'); // another route

Controller

    public function profile() {
         if (Auth::check() && Auth::user()->is_admin) {
           $test = app('AppHttpControllersAdminController')->getshow();

          }
         elseif (Auth::check() && Auth::user()->is_superadmin) {
         $test = app('AppHttpControllersSuperAdminController')->getshow();
         // this must not return a view but it will return just the needed data , you can pass parameters like this `->getshow($param1,$param2)`

         }

        return View('profile')->with('data', $test);
           }

But i think its better to use a trait

trait Show {

    public function showadmin() {
    .....
    }
    public function showuser() {
    .....
    }
}

Then

class HomeController extends Controller {
     use Show;
}

Then you can do the same as the above but instead of

   $test = app('AppHttpControllersAdminController')->getshow();// or the other one

use this

$this->showadmin();
$this->showuser(); // and use If statment ofc
Saturday, May 29, 2021
 
eek
answered 5 Months ago
eek
23

The web enabled extensions and command line enabled extensions can differ. Run php -m in your terminal and check to see if mcrypt is listed. If it's not then check where the command line is loading your php.ini file from by running php --ini from your terminal.

In this php.ini file you can enable the extension.

OSX

I have heard of people on OSX running in to problems due to the terminal pointing to the native PHP shipped with OSX. You should instead update your bash profile to include the actual path to your PHP. Something like this (I don't actually use OSX so this might not be 100%):

export PATH=/usr/local/php5/bin:$PATH

Ubuntu

On earlier versions of Ubuntu (prior to 14.04) when you run sudo apt-get install php5-mcrypt it doesn't actually install the extension into the mods-available. You'll need to symlink it.

sudo ln -s /etc/php5/conf.d/mcrypt.ini /etc/php5/mods-available/mcrypt.ini

On all Ubuntu versions you'll need to enable the mod once it's installed. You can do that with php5enmod.

sudo php5enmod mcrypt
sudo service apache2 restart

NOTES

  • PHP 7.1 deprecated mcrypt and 7.2 has removed the mcrypt extension entirely
  • Laravel 5.1 and later removed the need for mcrypt
Tuesday, June 1, 2021
 
davidb
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :