Viewed   128 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

1

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, October 12, 2022
4

In Laravel 5, you can use the helper methods:

return redirect()->route('route.name', [$param]);
Friday, August 19, 2022
1

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__);
}
Tuesday, August 9, 2022
 
3

First start from blade (please check comments):

@extends('layout')
@section('content')
    <div class="row">
        <table class="table table-hover table-striped table-dark" id="slugTb">
            <thead>
            <tr>
                <th scope="col"><input type="checkbox" id="checkHead" class="selectall"></th>
                <th scope="col">Id</th>
                <th scope="col">Day</th>
                <th scope="col">Title</th>
                <th scope="col">Description</th>
            </tr>
            </thead>
        <tbody></tbody>
        </table>
    </div>
@endsection

@section('scripts')
    <script>
        $(document).ready(function() {
            var query = 'damnSon';
            $.ajax({
                url: "{{ route('test.action') }}",
                method: 'GET',
                data: {
                    'slug': '{{ $packageSlug }}',
                    'query': query
                },
                dataType: 'json',
            })
                .done(function(data) {
                    console.log(data) //use console.log to debug
                    $('#slugTb tbody').html(data.table_data); //set table id so that you don't miss the right one
            })
                .fail(function(err) {
                    console.log(err) //in case if error happens
            })
                .always(function() {
                    console.log( "complete" ); //result despite the response code
            });
        });

    </script>
@endsection

You used deprecated jquery method like success check
Better use this three: done,fail,always

Next route web.php:

Route::get('action', ['as' => 'test.action', 'uses' => 'TestController@action']);

In your case better to use Request params bag so that you can add as much params as you want.

Next controller:

function action(Request $request)
{
    $total_row = 1;

    $packageSlug = $request->get('slug'); //names that you set in ajax data tag: {'slug': '{{ $packageSlug }}','query': query}
    $query = $request->get('query');
    $output = '<tr>
       <td align="center" colspan="1">' . $packageSlug . '</td>
       <td align="center" colspan="1">' . $query .'</td>
      </tr>';

        $data = array(
            'table_data'  => $output,
            'total_data'  => $total_row
        );

        return response()->json($data);
}

You should return something from the controller so blade can show that data and json encode it so that js could parse it. That is why return response()->json($data);

Other way:

route:

Route::get('/action/{slug}/{query}',['as' => 'test.action', 'uses' => 'TestController@action']);

blade script:

<script>
    $(document).ready(function() {
        var query = 'damnSon';
        $.ajax({
            url: 'action/{{ $packageSlug }}/' + query,
            method: 'GET',
            dataType: 'json',
        })
            .done(function(data) {
                console.log(data) //use console.log to debug
                $('#slugTb tbody').html(data.table_data); //set table id so that you don't miss the right one
        })
            .fail(function(err) {
                console.log(err) //in case if error happens
        })
            .always(function() {
                console.log( "complete" ); //result despite the response code
        });
    });

</script>

and controller:

function action($slug, $query)
{
    $total_row = 1;

    $packageSlug = $slug;
    $query = $query;
    $output = '<tr>
       <td align="center" colspan="1">' . $packageSlug . '</td>
       <td align="center" colspan="1">' . $query .'</td>
      </tr>';

        $data = array(
            'table_data'  => $output,
            'total_data'  => $total_row
        );

        return response()->json($data);
}

Not recommended just because you manually type route in ajax request: url: 'action/{{ $packageSlug }}/' + query if your route changes you have to change it in js.

Thursday, September 1, 2022
2

From PSR-4 specification:

All class names MUST be referenced in a case-sensitive fashion.

So you'll need to rename your modules and controllers folders to Modules and Controllers respectively.

So it becomes:

app
- ...
- Modules
-- ModuleName
--- Controllers
---- BackendController.php
...

I wouldn't recommend renaming your namespaces to lowercase names because that just breaks the consistency in your code and project structure. It will be a headache to maintain and figure out which part of your namespace needs to be capitalized which one doesn't.

Friday, October 7, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :